ficsit-cli-flake/cmd/root.go

141 lines
4 KiB
Go
Raw Normal View History

2021-11-05 21:42:49 +00:00
package cmd
import (
2021-12-02 04:00:33 +00:00
"io"
"os"
2022-04-14 01:27:39 +00:00
"path"
2021-12-02 04:00:33 +00:00
"path/filepath"
2022-04-14 01:27:39 +00:00
"runtime"
2021-12-02 04:00:33 +00:00
"time"
"github.com/pkg/errors"
2021-11-05 21:42:49 +00:00
"github.com/pterm/pterm"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var rootCmd = &cobra.Command{
Use: "ficsit",
Short: "cli mod manager for satisfactory",
2021-12-02 04:00:33 +00:00
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2021-11-05 21:42:49 +00:00
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetEnvPrefix("ficsit")
viper.AutomaticEnv()
_ = viper.ReadInConfig()
level, err := zerolog.ParseLevel(viper.GetString("log"))
if err != nil {
panic(err)
}
zerolog.SetGlobalLevel(level)
2021-12-02 04:00:33 +00:00
writers := make([]io.Writer, 0)
2021-11-05 21:42:49 +00:00
if viper.GetBool("pretty") {
pterm.EnableStyling()
2021-12-02 04:00:33 +00:00
} else {
pterm.DisableStyling()
}
if !viper.GetBool("quiet") {
writers = append(writers, zerolog.ConsoleWriter{
2021-11-05 21:42:49 +00:00
Out: os.Stdout,
TimeFormat: time.RFC3339,
})
}
2021-12-02 04:00:33 +00:00
if viper.GetString("log-file") != "" {
2022-05-02 20:07:15 +00:00
logFile, err := os.OpenFile(viper.GetString("log-file"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0777)
2021-12-02 04:00:33 +00:00
if err != nil {
return errors.Wrap(err, "failed to open log file")
}
writers = append(writers, logFile)
}
log.Logger = zerolog.New(io.MultiWriter(writers...)).With().Timestamp().Logger()
return nil
2021-11-05 21:42:49 +00:00
},
}
func Execute() {
// Execute tea as default
cmd, _, err := rootCmd.Find(os.Args[1:])
2021-12-02 04:00:33 +00:00
2021-12-04 18:02:05 +00:00
// Allow opening via explorer
cobra.MousetrapHelpText = ""
2021-12-02 04:00:33 +00:00
cli := len(os.Args) >= 2 && os.Args[1] == "cli"
2021-11-05 21:42:49 +00:00
if (len(os.Args) <= 1 || os.Args[1] != "help") && (err != nil || cmd == rootCmd) {
2021-12-02 04:00:33 +00:00
args := append([]string{"cli"}, os.Args[1:]...)
rootCmd.SetArgs(args)
cli = true
}
// Always be quiet in CLI mode
if cli {
viper.Set("quiet", true)
2021-11-05 21:42:49 +00:00
}
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
func init() {
2022-04-14 01:27:39 +00:00
var baseLocalDir string
switch runtime.GOOS {
case "windows":
baseLocalDir = os.Getenv("APPDATA")
case "linux":
baseLocalDir = path.Join(os.Getenv("HOME"), ".local", "share")
default:
panic("unsupported platform: " + runtime.GOOS)
}
viper.Set("base-local-dir", baseLocalDir)
2021-11-05 21:42:49 +00:00
baseCacheDir, err := os.UserCacheDir()
if err != nil {
panic(err)
}
rootCmd.PersistentFlags().String("log", "info", "The log level to output")
2021-12-02 04:00:33 +00:00
rootCmd.PersistentFlags().String("log-file", "", "File to output logs to")
rootCmd.PersistentFlags().Bool("quiet", false, "Do not log anything to console")
2021-11-05 21:42:49 +00:00
rootCmd.PersistentFlags().Bool("pretty", true, "Whether to render pretty terminal output")
2021-12-02 04:00:33 +00:00
rootCmd.PersistentFlags().Bool("dry-run", false, "Dry-run. Do not save any changes")
rootCmd.PersistentFlags().String("cache-dir", filepath.Clean(filepath.Join(baseCacheDir, "ficsit")), "The cache directory")
2022-04-14 01:27:39 +00:00
rootCmd.PersistentFlags().String("local-dir", filepath.Clean(filepath.Join(baseLocalDir, "ficsit")), "The local directory")
2021-12-02 04:00:33 +00:00
rootCmd.PersistentFlags().String("profiles-file", "profiles.json", "The profiles file")
rootCmd.PersistentFlags().String("installations-file", "installations.json", "The installations file")
2022-05-02 20:07:15 +00:00
rootCmd.PersistentFlags().String("api-base", "https://api.ficsit.app", "URL for API")
rootCmd.PersistentFlags().String("graphql-api", "/v2/query", "Path for GraphQL API")
2021-11-05 21:42:49 +00:00
_ = viper.BindPFlag("log", rootCmd.PersistentFlags().Lookup("log"))
2021-12-02 04:00:33 +00:00
_ = viper.BindPFlag("log-file", rootCmd.PersistentFlags().Lookup("log-file"))
_ = viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet"))
2021-11-05 21:42:49 +00:00
_ = viper.BindPFlag("pretty", rootCmd.PersistentFlags().Lookup("pretty"))
2021-12-02 04:00:33 +00:00
_ = viper.BindPFlag("dry-run", rootCmd.PersistentFlags().Lookup("dry-run"))
2021-11-05 21:42:49 +00:00
_ = viper.BindPFlag("cache-dir", rootCmd.PersistentFlags().Lookup("cache-dir"))
2022-04-14 01:27:39 +00:00
_ = viper.BindPFlag("local-dir", rootCmd.PersistentFlags().Lookup("local-dir"))
2021-12-02 04:00:33 +00:00
_ = viper.BindPFlag("profiles-file", rootCmd.PersistentFlags().Lookup("profiles-file"))
_ = viper.BindPFlag("installations-file", rootCmd.PersistentFlags().Lookup("installations-file"))
2022-05-02 20:07:15 +00:00
_ = viper.BindPFlag("api-base", rootCmd.PersistentFlags().Lookup("api-base"))
_ = viper.BindPFlag("graphql-api", rootCmd.PersistentFlags().Lookup("graphql-api"))
2021-11-05 21:42:49 +00:00
}