Go cli demo checkin
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
Copyright © 2026 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go_cli/internal/reindeer"
|
||||
)
|
||||
|
||||
// danceCmd represents the dance command
|
||||
var danceCmd = &cobra.Command{
|
||||
Use: "dance",
|
||||
Short: "Make the reindeer dance for my amusement.",
|
||||
Long: `Makes many dancing reindeer.`,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("pre-running.")
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
duration := viper.GetInt("duration")
|
||||
fmt.Printf("reindeer are now dancing for %d",duration)
|
||||
for i:=1;i<= duration;i++ {
|
||||
// prints left dancing reindeer
|
||||
time.Sleep(time.Millisecond*500)
|
||||
fmt.Printf("%s\r",reindeer.DanceLeft)
|
||||
fmt.Print("\033[14A")
|
||||
|
||||
// prints right dancing reindeer
|
||||
time.Sleep(time.Millisecond*500)
|
||||
fmt.Printf("%s\r",reindeer.DanceRight)
|
||||
fmt.Print("\033[14A")
|
||||
|
||||
}
|
||||
fmt.Print(strings.Repeat("\n",15))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(danceCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// danceCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// danceCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
Copyright © 2026 carl.macy.stanton@protonmail.com
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "info",
|
||||
Short: "Show how to use this application.",
|
||||
Long: `Lists all of the possible options to run.`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// 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/.go_cli.yaml)")
|
||||
|
||||
// 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")
|
||||
rootCmd.AddCommand(danceCmd)
|
||||
danceCmd.Flags().IntP("duration","d",10,"Specify how long to dance.")
|
||||
_ = viper.BindPFlags(danceCmd.Flags())
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// Search config in home directory with name ".go_cli" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".go_cli")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user