2016-07-27 17:38:10 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2016-07-27 21:15:49 +00:00
|
|
|
gogs "github.com/gogits/go-gogs-client"
|
2016-07-27 17:38:10 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cfgFile string
|
|
|
|
var apiURL string
|
2016-07-27 21:15:49 +00:00
|
|
|
var tokenArg string
|
|
|
|
var client *gogs.Client
|
2016-07-27 17:38:10 +00:00
|
|
|
|
|
|
|
// RootCmd represents the base command when called without any subcommands
|
|
|
|
var RootCmd = &cobra.Command{
|
|
|
|
Use: "gogs",
|
|
|
|
Short: "Connect to the Gogs API.",
|
|
|
|
Long: `Welcome to the Gogs CLI.
|
|
|
|
|
2016-08-02 11:10:10 +00:00
|
|
|
$ 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]
|
|
|
|
`,
|
2016-07-27 17:38:10 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-07-28 14:03:11 +00:00
|
|
|
token := viper.GetString("GOGS_TOKEN")
|
2016-07-27 17:38:10 +00:00
|
|
|
if token != "" {
|
2016-07-28 00:11:57 +00:00
|
|
|
fmt.Println("Token authentication enabled.")
|
2016-07-27 17:38:10 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println("No token found.")
|
|
|
|
}
|
|
|
|
|
2016-07-28 14:03:11 +00:00
|
|
|
url := viper.GetString("GOGS_URL")
|
2016-07-27 17:38:10 +00:00
|
|
|
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() {
|
2016-07-27 21:15:49 +00:00
|
|
|
cobra.OnInitialize(initConfig, initClient)
|
|
|
|
|
2016-07-27 17:38:10 +00:00
|
|
|
// 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-cli.yaml)")
|
2016-07-28 14:03:11 +00:00
|
|
|
RootCmd.PersistentFlags().StringVar(&apiURL, "url", viper.GetString("GOGS_URL"), "api url should include /api/v1 path (default is try.gogs.io/api/v1)")
|
|
|
|
RootCmd.PersistentFlags().StringVar(&tokenArg, "token", viper.GetString("GOGS_TOKEN"), "token authorization (if not specified in cfg file)")
|
2016-07-27 21:15:49 +00:00
|
|
|
|
2016-07-27 17:38:10 +00:00
|
|
|
// Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
|
|
|
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
}
|
|
|
|
|
2016-07-27 22:06:32 +00:00
|
|
|
func GetClient() *gogs.Client {
|
2016-07-28 10:46:02 +00:00
|
|
|
// fmt.Println("Getting client....")
|
2016-07-27 22:06:32 +00:00
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func initClient() {
|
2016-07-28 14:03:11 +00:00
|
|
|
url := viper.GetString("GOGS_URL")
|
|
|
|
token := viper.GetString("GOGS_TOKEN")
|
2016-07-27 22:06:32 +00:00
|
|
|
client = gogs.NewClient(url, token)
|
|
|
|
}
|
|
|
|
|
2016-07-27 17:38:10 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2016-07-28 00:11:57 +00:00
|
|
|
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
|
2016-07-27 17:38:10 +00:00
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
2016-07-28 00:11:57 +00:00
|
|
|
// fmt.Println("Using config file:", viper.ConfigFileUsed())
|
2016-07-27 17:38:10 +00:00
|
|
|
} else {
|
2016-07-28 14:16:20 +00:00
|
|
|
fmt.Println("No configuration file found. Is there for sure one at $HOME/.go-gogs-cli.yaml?")
|
2016-07-27 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|