refactored, added completion command

This commit is contained in:
2019-08-14 14:06:45 +02:00
parent e6c1f2b577
commit fa98c1495b
13 changed files with 229 additions and 98 deletions

29
cmd/gogs/completion.go Normal file
View File

@ -0,0 +1,29 @@
package main
import (
"errors"
"os"
"github.com/spf13/cobra"
)
var completionCmd = &cobra.Command{
Use: "completion bash|zsh",
Short: "Generate shell completion",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
shell := args[0]
switch shell {
case "bash":
return cmd.Parent().GenBashCompletion(os.Stdout)
case "zsh":
return cmd.Parent().GenZshCompletion(os.Stdout)
default:
return errors.New("valid shell are bash or zsh")
}
},
}
func init() {
RootCmd.AddCommand(completionCmd)
}

19
cmd/gogs/main.go Normal file
View File

@ -0,0 +1,19 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
func main() {
Execute()
}

149
cmd/gogs/repo-create.go Normal file
View File

@ -0,0 +1,149 @@
package main
import (
"fmt"
"os/exec"
"strings"
"github.com/gogits/go-gogs-client"
"github.com/spf13/cobra"
)
// Flags.
var repoName string
var repoDescription string
var repoIsPrivate bool
var orgName string
var repoRemoteName string
var createCmd = &cobra.Command{
Aliases: []string{"new", "n", "c"},
Use: "create <name>",
Short: "Create a repository",
Example: `
gogs repo create my-new-repo
gogs repo new my-new-repo
gogs repo create -n=my-new-repo
gogs repo create JustUsGuys/our-new-repo --desc="a thing with things" -p=true
gogs repo new my-new-repo --private
gogs repo create my-new-repo --add-remote=origin Will initialize git if not already, too.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("Please argue me a name of a repo to create.")
return
}
// Moving to use MyCompany/new-project as way to specify organization instead of
// --org flag.
if len(args) == 1 {
splitter := strings.Split(args[0], "/")
if len(splitter) == 2 {
orgName = splitter[0]
repoName = splitter[1]
} else {
repoName = args[0]
}
} else {
// we got MyCompany new-project
orgName = args[0]
repoName = args[1] // this'll take 'cactus' of MyCompany cactus spikey --private; spikey will be igored... FIXME?
}
createRepoOpts := gogs.CreateRepoOption{
Name: repoName,
Description: repoDescription,
Private: repoIsPrivate,
}
var err error
var repo *gogs.Repository
if orgName == "" {
repo, err = GetClient().CreateRepo(createRepoOpts)
} else {
repo, err = GetClient().CreateOrgRepo(orgName, createRepoOpts)
}
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Repo created! Woohoo!")
printRepo(repo)
// add git url as remote to working dir
if repoRemoteName != "" {
// get git path
getGitComm := exec.Command("/usr/bin/which", "git")
whichGit, err := getGitComm.Output()
if err != nil {
fmt.Println(err)
fmt.Println("...You have git installed, right?")
return
}
whichGitClean := strings.Replace(string(whichGit), "\n", "", 1)
gitAddRemoteComm := exec.Command(whichGitClean, "remote", "add", repoRemoteName, repo.CloneURL)
_, err = gitAddRemoteComm.Output()
if err != nil {
// go a step further and let's try init-ing the repo
//
var gitInitDone = make(chan bool)
go func() {
gitInitComm := exec.Command(whichGitClean, "init")
gitInit, initErr := gitInitComm.Output()
if initErr != nil {
fmt.Println(initErr)
} else {
fmt.Println(string(gitInit))
}
gitInitDone <- true
}()
// wait for gitInitDone
<-gitInitDone
// Apparently exec can only call any given command once.
// https://github.com/golang/go/issues/10305
gitAddRemoteComm2 := exec.Command(whichGitClean, "remote", "add", repoRemoteName, repo.CloneURL)
_, err = gitAddRemoteComm2.Output()
if err != nil {
fmt.Println("error adding remote -- ", err.Error())
} else {
gitShowRemotesCommand := exec.Command(whichGitClean, "remote", "-v")
gitShowRemotes, err := gitShowRemotesCommand.Output()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(gitShowRemotes))
}
} else {
// else there was git already and remote was added
// fmt.Println(string(addRemote)) // gotcha: adding a remote success returns ""
// fmt.Println("Remote added as " + repoRemoteName)
gitShowRemotesCommand2 := exec.Command(whichGitClean, "remote", "-v")
gitShowRemotes2, err := gitShowRemotesCommand2.Output()
if err != nil {
fmt.Println(err)
}
fmt.Println(string(gitShowRemotes2))
}
}
},
}
func init() {
repoCmd.AddCommand(createCmd)
// createCmd.Flags().StringVarP(&repoName, "name", "n", "", "repo name")
createCmd.Flags().StringVarP(&repoDescription, "desc", "d", "", "repo description")
createCmd.Flags().BoolVarP(&repoIsPrivate, "private", "p", false, "repo is private")
// createCmd.Flags().StringVarP(&orgName, "org", "o", "", "organization")
createCmd.Flags().StringVarP(&repoRemoteName, "add-remote", "r", "", "remote name")
}

63
cmd/gogs/repo-destroy.go Normal file
View File

@ -0,0 +1,63 @@
package main
import (
"fmt"
"strings"
"github.com/spf13/cobra"
)
// destroyCmd represents the destroy command
var destroyCmd = &cobra.Command{
Aliases: []string{"d", "delete", "rid"},
Use: "destroy [username repo-name | username/repo-name]",
Short: "Destroy a repo.",
Example: `
destroy ia tester-repo
destroy ia/tester-repo
`,
Run: func(cmd *cobra.Command, args []string) {
var owner string
var repo string
// ia/testes || ia testes
if (len(args) == 0) || (len(args) > 2) {
fmt.Println("Please argue me [username/repo-name] or [username repo-name].")
return
}
// ia/testes
if len(args) == 1 {
slasher := strings.Split(args[0], "/")
if len(slasher) == 2 {
owner, repo = slasher[0], slasher[1]
} else {
fmt.Println("Please argue me [username/repo-name] or [username repo-name].")
return
}
}
// ia testes
if len(args) == 2 {
owner, repo = args[0], args[1]
}
if (owner == "") || (repo == "") {
fmt.Println("Please argue me [username/repo-name] or [username repo-name].")
return
}
err := GetClient().DeleteRepo(owner, repo)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Deleted %v/%v.\n\n", owner, repo)
},
}
func init() {
repoCmd.AddCommand(destroyCmd)
}

28
cmd/gogs/repo-list.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"fmt"
"github.com/spf13/cobra"
)
var listCmd = &cobra.Command{
Use: "list",
Short: "List your repositories.",
Long: "List all your repositories.",
Run: func(cmd *cobra.Command, args []string) {
repos, err := GetClient().ListMyRepos()
if err != nil {
fmt.Println(err)
return
}
for _, repo := range repos {
printRepo(repo)
}
},
}
func init() {
repoCmd.AddCommand(listCmd)
}

91
cmd/gogs/repo-migrate.go Normal file
View File

@ -0,0 +1,91 @@
package main
import (
"fmt"
"strings"
"github.com/gogits/go-gogs-client"
"github.com/spf13/cobra"
)
// Flags.
var migrateMirror bool
var migratePrivate bool
func unusableUsage() {
fmt.Println("Please argue me <user|organization>/<name-of-new-repo> <http://url.of.cloneable.repo.git>")
}
// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Aliases: []string{"m"},
Use: "migrate, m",
Short: "Migrate a repository from a given remote source.",
Example: `
gogs repo migrate irstacks/copycat https://github.com/gogits/gogs.git
gogs repo migrate myCompany/copycat https://github.com/gogits/gogs.git
`,
Run: func(cmd *cobra.Command, args []string) {
var opts gogs.MigrateRepoOption
if len(args) != 2 {
unusableUsage()
return
}
ownerSlashReponame := args[0]
migrateUrl := args[1]
// irstacks/my-copy-cat --> [irstacks, my-copy-cat]
ownernameReponame := strings.Split(ownerSlashReponame, "/")
if len(ownernameReponame) != 2 {
unusableUsage()
return
}
// get "userId" for owner name from extracurricular function...
// if the user return err, then we'll need to check if an organization was intended.
// this is an inconvenience by gogs api client. they should more explicity specify ownership
// in the migrateRepoOptions.
user, err := getUserByName(ownernameReponame[0])
if err != nil {
// I don't think it actually ever goes here...
// which means getUserByName works as well for orgs. huh.
fmt.Println(err)
fmt.Println("Searching for an org by than name...")
org, oerr := GetClient().GetOrg(ownernameReponame[0])
if oerr != nil {
fmt.Println("Could find neither user nor org by by the name: ", ownernameReponame[0])
fmt.Println(err)
return
} else {
opts.UID = int(org.ID)
}
} else {
opts.UID = int(user.ID)
}
opts.CloneAddr = migrateUrl
opts.RepoName = ownernameReponame[1]
opts.Mirror = migrateMirror
opts.Private = migratePrivate
repo, err := GetClient().MigrateRepo(opts)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Repo migrated! Woohoo!")
printRepo(repo)
},
}
func init() {
repoCmd.AddCommand(migrateCmd)
migrateCmd.Flags().BoolVarP(&migrateMirror, "mirror", "m", false, " make your migrated repo a mirror of the original")
migrateCmd.Flags().BoolVarP(&migratePrivate, "private", "p", false, " make your migrated repo private")
}

99
cmd/gogs/repo-search.go Normal file
View File

@ -0,0 +1,99 @@
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/gogits/go-gogs-client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Flags.
var limit string // max num results
var userName string
func searchRepos(uid, query, limit string) ([]*gogs.Repository, error) {
client := &http.Client{}
path := "/api/v1/repos/search?q=" + query + "&uid=" + uid + "&limit=" + limit
repos, err := getParsedResponse(client, "GET", viper.GetString("GOGS_URL")+path, nil, nil)
return repos, err
}
// searchCmd represents the search command
var searchCmd = &cobra.Command{
Aliases: []string{"s", "find", "f"},
Use: "search",
Short: "search myquery [-l -u]",
Example: `
gogs repo s waldo
gogs repo find waldo
gogs repo search waldo -l 5
gogs repo find waldo --user=johnny --limit=100
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("Please argue me something to search... like: $ gogs repo search golang")
fmt.Println("For help, run $ gogs repo search --help")
return
}
// uid := make(chan string)
uid := "0"
var u *gogs.User
var e error
// flag for user name, we'll need to get that user's uid
if userName != "" {
// go func() {
u, e = getUserByName(userName)
if e != nil {
fmt.Println(e)
}
uid = strconv.Itoa(int(u.ID))
// }()
}
repos, err := searchRepos(uid, args[0], limit)
if err != nil {
fmt.Println(err)
return
}
var describeUserScope string
if uid != "0" {
describeUserScope = u.UserName + "'s repos"
} else {
describeUserScope = "all repos"
}
fmt.Println("Searching '" + args[0] + "' in " + describeUserScope + "...")
for _, repo := range repos {
// get (mostly) empty data in, need to make n more calls to GetRepo... shit.
splitter := strings.Split(repo.FullName, "/")
owner := splitter[0]
reponame := splitter[1]
r, e := GetClient().GetRepo(owner, reponame)
if e != nil {
fmt.Println(e)
return
}
printRepo(r)
}
},
}
func init() {
repoCmd.AddCommand(searchCmd)
searchCmd.Flags().StringVarP(&userName, "user", "u", "", "whose repos to search in")
searchCmd.Flags().StringVarP(&limit, "limit", "l", "10", "limit number of results")
}

51
cmd/gogs/repo.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
"fmt"
"github.com/gogits/go-gogs-client"
"github.com/spf13/cobra"
)
// repoCmd represents the repo command
var repoCmd = &cobra.Command{
Aliases: []string{"r"},
Use: "repo",
Short: "parent command for repositories",
Example: `
gogs repo new my-new-repo --private
gogs repo create my-new-repo --org=JustUsGuys
gogs repo list
gogs repo migrate ia/my-copy-cat https://github.com/gogits/gogs.git
gogs repo destroy ia my-new-repo
gogs repo destroy ia/my-new-repo
`,
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("Please use: gogs repo [(new|create)|list|destroy]")
// },
}
func printRepo(repo *gogs.Repository) {
fmt.Println()
fmt.Printf("| * %v", repo.FullName)
if repo.Private {
fmt.Printf(" (private)")
}
if repo.Fork {
fmt.Printf(" (fork)")
}
fmt.Println("")
if repo.Description != "" {
fmt.Println("| --> ", repo.Description)
}
fmt.Println("| HTML: ", repo.HTMLURL)
fmt.Println("| SSH : ", repo.SSHURL)
fmt.Println("| GIT : ", repo.CloneURL)
fmt.Println()
}
func init() {
RootCmd.AddCommand(repoCmd)
}

95
cmd/gogs/root.go Normal file
View File

@ -0,0 +1,95 @@
package main
import (
"fmt"
"os"
gogs "github.com/gogits/go-gogs-client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var apiURLArg string
var tokenArg string
var client *gogs.Client
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "gogs",
Short: "Connect to the Gogs API.",
Run: func(cmd *cobra.Command, args []string) {
token := viper.GetString("GOGS_TOKEN")
if token != "" {
fmt.Println("Token authentication enabled.")
} else {
fmt.Println("No token found.")
}
url := viper.GetString("GOGS_URL")
if url != "" {
fmt.Println("Using API url @ ", url)
} else {
fmt.Println("No API url coming through... uh oh.")
}
},
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func init() {
cobra.OnInitialize(initConfig, initClient)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gogs.yaml)")
RootCmd.PersistentFlags().StringVar(&apiURLArg, "url", "", "api url should include /api/v1 path (default is try.gogs.io/api/v1)")
RootCmd.PersistentFlags().StringVar(&tokenArg, "token", "", "token authorization (if not specified in cfg file)")
}
func GetClient() *gogs.Client {
return client
}
func initClient() {
url := viper.GetString("GOGS_URL")
token := viper.GetString("GOGS_TOKEN")
client = gogs.NewClient(url, token)
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag
viper.SetConfigFile(cfgFile)
}
viper.SetConfigName(".gogs") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
// fmt.Println("Using config file:", viper.ConfigFileUsed())
} else {
fmt.Println("No configuration file found. Is there for sure one at $HOME/.gogs.yaml?")
}
// These should override any configFile or env vars.
if tokenArg != "" {
viper.Set("token", tokenArg)
}
if apiURLArg != "" {
viper.Set("url", apiURLArg)
}
}

89
cmd/gogs/util.go Normal file
View File

@ -0,0 +1,89 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
gogs "github.com/gogits/go-gogs-client"
"github.com/spf13/viper"
)
// Useful for getting dat uid.
func getUserByName(userName string) (user *gogs.User, err error) {
user, err = GetClient().GetUserInfo(userName)
if err != nil {
fmt.Println(err)
return
}
return user, err
}
// This sucker is really annoying. Gogs Client says that search is a thing.
// And it is, it does work.
// But... I can't find it in the package. It's just not there. I've looked.
// So this is a re-write of the respondering methods that are nonexported in
// the Gogs Client package. Here they're used exclusively (and tweaked a little, as such)
// for the search command.
// It's dumb. I know.
type expRes struct {
Data []*gogs.Repository `json:"data"`
}
func getResponse(client *http.Client, method, url string, header http.Header, body io.Reader) ([]byte, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "token "+viper.GetString("GOGS_TOKEN"))
for k, v := range header {
req.Header[k] = v
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 403:
return nil, errors.New("403 Forbidden")
case 404:
return nil, errors.New("404 Not Found")
}
if resp.StatusCode/100 != 2 {
errMap := make(map[string]interface{})
if err = json.Unmarshal(data, &errMap); err != nil {
return nil, err
}
return nil, errors.New(errMap["message"].(string))
}
return data, nil
}
func getParsedResponse(client *http.Client, method, path string, header http.Header, body io.Reader) ([]*gogs.Repository, error) {
data, err := getResponse(client, method, path, header, body)
if err != nil {
return nil, err
}
var res expRes
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res.Data, err
}