67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
const VERSION string = "1.2"
|
|
const CONFIG_DIR string = "/.config/gitw"
|
|
const LOCAL_CONFIG_DIR string = "./.gitw"
|
|
|
|
var (
|
|
version bool
|
|
dry_run bool
|
|
rootCmd = &cobra.Command{
|
|
Use: "gitw",
|
|
Short: "Git issue and milestone integration for taskwarrior",
|
|
Long: `Gitwarrior is a CLI tool to extend the taskwarrior tasks to synch issues and milestones.
|
|
Gitwarrior requires the following uda's to be defined: last_gitw_update(date), git_number(number), git_type(string), git_comment_ids(string).`,
|
|
}
|
|
)
|
|
|
|
func Execute() error {
|
|
return rootCmd.Execute()
|
|
}
|
|
|
|
func init() {
|
|
cobra.OnInitialize(initConfig)
|
|
// version flag
|
|
rootCmd.Flags().BoolVarP(&version, "version", "v", false, "Print version of gitwarrior")
|
|
rootCmd.Version = VERSION
|
|
}
|
|
|
|
func initConfig() {
|
|
home := os.Getenv("HOME")
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath(LOCAL_CONFIG_DIR)
|
|
viper.AddConfigPath(home + CONFIG_DIR)
|
|
viper.AutomaticEnv()
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
// config file not found, create starting configuration file for the user
|
|
viper.Set("base_url", "<git-http-base-url>")
|
|
viper.Set("user_name", "<git-user-name>")
|
|
viper.Set("access_code", "<git-access-code>")
|
|
// write default configuration in configuration directory inside of HOME directory
|
|
if err = os.MkdirAll(home+CONFIG_DIR, os.ModePerm); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Could not create directories for configuration file: %s\nError: %s\n", home+CONFIG_DIR, err)
|
|
os.Exit(-1)
|
|
}
|
|
if err = viper.SafeWriteConfigAs(home + CONFIG_DIR + "config.yaml"); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Failed to write starting configuration file: %s\n", err)
|
|
os.Exit(-1)
|
|
}
|
|
fmt.Printf("Could not find configuration file in %s\nCreated starting configuration for you. Please enter the necessary values accordingly and try again.\n", home+CONFIG_DIR)
|
|
os.Exit(2)
|
|
} else {
|
|
fmt.Fprintf(os.Stderr, "Failed to read configuration file: %s\n", err)
|
|
os.Exit(-1)
|
|
}
|
|
}
|
|
}
|