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()
}

View File

@ -1,4 +1,4 @@
package cmd
package main
import (
"fmt"
@ -21,14 +21,13 @@ var createCmd = &cobra.Command{
Aliases: []string{"new", "n", "c"},
Use: "create <name>",
Short: "Create a repository",
Long: `create my-new-repo | MyOrg/our-new-repo [-d | --desc] [-p | --private] [-r | --add-remote]]
$ 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.`,
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 {
@ -86,7 +85,7 @@ var createCmd = &cobra.Command{
whichGitClean := strings.Replace(string(whichGit), "\n", "", 1)
gitAddRemoteComm := exec.Command(whichGitClean, "remote", "add", repoRemoteName, repo.CloneUrl)
gitAddRemoteComm := exec.Command(whichGitClean, "remote", "add", repoRemoteName, repo.CloneURL)
_, err = gitAddRemoteComm.Output()
if err != nil {
@ -106,23 +105,22 @@ var createCmd = &cobra.Command{
}()
// wait for gitInitDone
select {
case <-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()
<-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("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))
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 ""

View File

@ -1,4 +1,4 @@
package cmd
package main
import (
"fmt"
@ -12,14 +12,9 @@ var destroyCmd = &cobra.Command{
Aliases: []string{"d", "delete", "rid"},
Use: "destroy [username repo-name | username/repo-name]",
Short: "Destroy a repo.",
Long: `destroy [username repo-name | username/repo-name]
$ destroy ia tester-repo
$ destroy ia/tester-repo
**CAREFUL!** YOU WON'T BE ASKED TWICE.
YE BE WARNED.
Example: `
destroy ia tester-repo
destroy ia/tester-repo
`,
Run: func(cmd *cobra.Command, args []string) {

View File

@ -1,4 +1,4 @@
package cmd
package main
import (
"fmt"

View File

@ -1,4 +1,4 @@
package cmd
package main
import (
"fmt"
@ -14,7 +14,6 @@ var migratePrivate bool
func unusableUsage() {
fmt.Println("Please argue me <user|organization>/<name-of-new-repo> <http://url.of.cloneable.repo.git>")
return
}
// migrateCmd represents the migrate command
@ -22,14 +21,9 @@ var migrateCmd = &cobra.Command{
Aliases: []string{"m"},
Use: "migrate, m",
Short: "Migrate a repository from a given remote source.",
Long: `Usage:
$ gogs repo migrate irstacks/copycat https://github.com/gogits/gogs.git
$ gogs repo migrate myCompany/copycat https://github.com/gogits/gogs.git
Options:
[-m | --mirror] If to be mirrored repo. (no arguments),
[-p | --private] If to be private repo. (no args)
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) {

View File

@ -1,4 +1,4 @@
package cmd
package main
import (
"fmt"
@ -30,19 +30,11 @@ var searchCmd = &cobra.Command{
Aliases: []string{"s", "find", "f"},
Use: "search",
Short: "search myquery [-l -u]",
Long: `Search repos by keyword, with optional flags for [-l | --limit] and [-u | --user]
$ gogs repo s waldo
$ gogs repo find waldo
$ gogs repo search waldo -l 5
$ gogs repo find waldo --user=johnny --limit=100
Default limit is 10.
Default all users' repos.
** NOTE that in order to search PRIVATE repos, you'll need to provide the
USERNAME FLAG
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) {
@ -53,7 +45,7 @@ USERNAME FLAG
}
// uid := make(chan string)
var uid string
uid := "0"
var u *gogs.User
var e error
@ -63,12 +55,9 @@ USERNAME FLAG
u, e = getUserByName(userName)
if e != nil {
fmt.Println(e)
uid = "0"
}
uid = strconv.Itoa(int(u.ID))
// }()
} else {
uid = "0"
}
repos, err := searchRepos(uid, args[0], limit)

View File

@ -1,4 +1,4 @@
package cmd
package main
import (
"fmt"
@ -12,15 +12,13 @@ var repoCmd = &cobra.Command{
Aliases: []string{"r"},
Use: "repo",
Short: "parent command for repositories",
Long: `gogs repo [(new|create)|list|destroy]
$ 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
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]")
@ -42,9 +40,9 @@ func printRepo(repo *gogs.Repository) {
fmt.Println("| --> ", repo.Description)
}
fmt.Println("| HTML: ", repo.HtmlUrl)
fmt.Println("| SSH : ", repo.SshUrl)
fmt.Println("| GIT : ", repo.CloneUrl)
fmt.Println("| HTML: ", repo.HTMLURL)
fmt.Println("| SSH : ", repo.SSHURL)
fmt.Println("| GIT : ", repo.CloneURL)
fmt.Println()
}

View File

@ -1,4 +1,4 @@
package cmd
package main
import (
"fmt"
@ -18,19 +18,6 @@ var client *gogs.Client
var RootCmd = &cobra.Command{
Use: "gogs",
Short: "Connect to the Gogs API.",
Long: `Welcome to the Gogs CLI.
$ gogs
--config path to config file (default $HOME/.go-gogs-cli.yaml; or you can set GOGS_TOKEN and GOGS_URL env vars)
--config="$HOME/.my-own-gogs-cli-file.yaml"
$ gogs [repo|r]
$ gogs [create|c|new|n]
[migrate|m]
[list]
[search|find|s|f]
[delete|destroy|rid|d]
`,
Run: func(cmd *cobra.Command, args []string) {
token := viper.GetString("GOGS_TOKEN")
if token != "" {
@ -64,7 +51,7 @@ func init() {
// 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-cli.yaml)")
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)")
@ -86,15 +73,15 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
}
viper.SetConfigName(".go-gogs-cli") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
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/.go-gogs-cli.yaml?")
fmt.Println("No configuration file found. Is there for sure one at $HOME/.gogs.yaml?")
}
// These should override any configFile or env vars.

View File

@ -1,4 +1,4 @@
package cmd
package main
import (
"encoding/json"