diff --git a/.gitignore b/.gitignore index e69de29..c056e6b 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +main.out diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f33e1a8 --- /dev/null +++ b/Makefile @@ -0,0 +1,54 @@ +BINARY_NAME:=main.out +GOBIN:=${HOME}/go/bin + + +# =================================== DEFAULT =================================== # + +default: all + +## default: Runs build and test +.PHONY: default +all: build test + +# =================================== HELPERS =================================== # + +## help: print this help message +.PHONY: help +help: + @echo 'Usage:' + @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' + +# =================================== DEVELOPMENT =================================== # + +## build: builds the binary +.PHONY: build +build: | + go build -o $(BINARY_NAME) main.go + +## test: Test the program +.PHONY: test +test: | + go test -v main.go + +# =================================== QUALITY ================================== # + +## tidy: Tidy mod file +.PHONY: tidy +tidy: | + go mod tidy -v + +## clean: Clean binaries +.PHONY: clean +clean: | + go clean && rm ${BINARY_NAME} + +## fmt: Format code +.PHONY: fmt +fmt: + go fmt ./... + +# https://golangci-lint.run/welcome/install/ +## lint: Lint code +.PHONY: lint +lint: + $(GOBIN)/golangci-lint run -v diff --git a/go.mod b/go.mod index 724c7a7..36c8332 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,29 @@ module github.com/caffeine-addictt/video-manager go 1.22.1 + +require ( + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.0 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/cobra v1.8.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.18.2 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/atomic v1.11.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4f0365c --- /dev/null +++ b/go.sum @@ -0,0 +1,61 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeBAsoHo= +github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= +go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw= +golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index e07c7a5..ab7414a 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,8 @@ package main -import "fmt" +import "github.com/caffeine-addictt/video-manager/src" +// Run the actual CLI in src/ func main() { - fmt.Println("Running!") + src.Execute() } diff --git a/src/logging.go b/src/logging.go new file mode 100644 index 0000000..0acd42e --- /dev/null +++ b/src/logging.go @@ -0,0 +1,15 @@ +package src + +import "fmt" + +func Info(message string) { + if debug || verbose { + fmt.Printf("INFO: %s\n", message) + } +} + +func Debug(message string) { + if debug { + fmt.Printf("DEBUG: %s\n", message) + } +} diff --git a/src/root.go b/src/root.go new file mode 100644 index 0000000..0399dee --- /dev/null +++ b/src/root.go @@ -0,0 +1,92 @@ +package src + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/caffeine-addictt/video-manager/src/utils" + "github.com/mitchellh/go-homedir" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// Global variables +var ( + cfgFile string + verbose bool + debug bool +) + +// Root command +var rootCommand = &cobra.Command{ + Use: "video-manager", + Short: "Video Manager CLI", + Long: utils.Multiline( + "Download, manage and view videos locally.", + "Documentation is available at https://github.com/caffeine-addictt/video-manager", + ), +} + +// Configuration +func init() { + cobra.OnInitialize(initConfig) + rootCommand.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.video-manager)") + + // Verbosity + rootCommand.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output") + rootCommand.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Debug output") +} + +func initConfig() { + home, err := homedir.Dir() + if err != nil { + fmt.Println("Failed to get home directory") + Debug(err.Error()) + os.Exit(1) + } + + if cfgFile != "" { + // Reading provided configuration file + viper.SetConfigFile(cfgFile) + Debug("-c supplied at " + cfgFile) + } else { + // Reading configuration file from either pwd or $HOME + viper.AddConfigPath(".") + viper.AddConfigPath(home) + viper.SetConfigName(".video-manager") + viper.SetConfigType("yaml") + Debug("-c not supplied, looking for configuration file in " + home + " or pwd") + } + + // Read file + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + fmt.Println("Configuration file not found") + + // Create configuration at $HOME if it does not exist + if cfgFile != "" { + os.Exit(1) + } + + if err := viper.SafeWriteConfigAs(filepath.Join(home, ".video-manager")); err != nil { + fmt.Println("Failed to create configuration file at " + home + "/.video-manager") + Debug(err.Error()) + os.Exit(1) + } + fmt.Println("Created default configuration file at " + home + "/.video-manager") + } else { + fmt.Println("Failed to read configuration file") + Debug(err.Error()) + os.Exit(1) + } + } + Info("Loaded configuration from " + viper.ConfigFileUsed()) +} + +func Execute() { + if err := rootCommand.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/src/utils/strings.go b/src/utils/strings.go new file mode 100644 index 0000000..7d475b5 --- /dev/null +++ b/src/utils/strings.go @@ -0,0 +1,7 @@ +package utils + +import "strings" + +func Multiline(line ...string) string { + return strings.Join(line, "\n") +} diff --git a/src/version.go b/src/version.go new file mode 100644 index 0000000..9715974 --- /dev/null +++ b/src/version.go @@ -0,0 +1,19 @@ +package src + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var versionCommand = &cobra.Command{ + Use: "version", + Short: "Print the version number of video-manager", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("v0.0.0") + }, +} + +func init() { + rootCommand.AddCommand(versionCommand) +}