From bd6ff2a9c0c0fdea0483f5a3c1c7b9a8ec37d922 Mon Sep 17 00:00:00 2001 From: Michael Savin Date: Sat, 29 Jun 2024 19:29:29 +0300 Subject: [PATCH] #13 - Add command lic --- cmd/lic.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 5 +++-- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 cmd/lic.go diff --git a/cmd/lic.go b/cmd/lic.go new file mode 100644 index 0000000..a1ba639 --- /dev/null +++ b/cmd/lic.go @@ -0,0 +1,62 @@ +package cmd + +import ( + "os" + "text/template" + "time" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var ( + // cbrfCmd represents the cbrf command + licCmd = &cobra.Command{ + Use: "lic", + Short: "Generate new WTFPL license for you project", + Long: `Just for fun! Generate new WTFPL license for LICENSE file with your email, full name and current year.`, + Run: func(cmd *cobra.Command, args []string) { + + Email := viper.GetString("email") + FullName := viper.GetString("full_name") + Year, _, _ := time.Now().Date() + tmpl, err := template.New("wtfpl").Parse(licenseTpl) + if err != nil { + panic(err) + } + + err = tmpl.Execute(os.Stdout, data{Email, FullName, Year}) + if err != nil { + panic(err) + } + }, + } +) + +func init() { + rootCmd.AddCommand(licCmd) + + // licCmd.Flags().BoolVarP(&showAll, "all", "a", false, "Show all rates") +} + +type data struct { + Email string + FullName string + Year int +} + +var licenseTpl = ` DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) {{ .Year }} {{ .FullName }} <{{ .Email }}> + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + +` diff --git a/cmd/root.go b/cmd/root.go index 5a252e5..84b850a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,7 +60,8 @@ func initConfig() { 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()) + err := viper.ReadInConfig() + if err != nil { + fmt.Printf("Error loading config: %s", err) } }