diff --git a/internal/config/command.go b/internal/config/command.go new file mode 100644 index 0000000..f540566 --- /dev/null +++ b/internal/config/command.go @@ -0,0 +1,6 @@ +package config + +type Command struct { + Name string + Args []string +} diff --git a/internal/config/commands.go b/internal/config/commands.go new file mode 100644 index 0000000..f7db8f6 --- /dev/null +++ b/internal/config/commands.go @@ -0,0 +1,19 @@ +package config + +import "errors" + +type Commands struct { + Commands map[string]func(*State, Command) error +} + +func (c *Commands) Register(name string, f func(*State, Command) error) { + c.Commands[name] = f +} + +func (c *Commands) Run(s *State, cmd Command) error { + f, ok := c.Commands[cmd.Name] + if !ok { + return errors.New("unknown command: " + cmd.Name) + } + return f(s, cmd) +} diff --git a/internal/config/config.go b/internal/config/config.go index 4135924..589d7c4 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,7 +3,6 @@ package config import ( "encoding/json" "os" - "os/user" ) const configFileName = ".gatorconfig.json" @@ -31,22 +30,9 @@ func Read() (Config, error) { } -func (config Config) SetUser() error { - usr, err := user.Current() - - if err != nil { - return err - } - - config.CurrentUserName = usr.Username - - err = write(config) - if err != nil { - return err - } - - return nil - +func (config Config) SetUser(username string) error { + config.CurrentUserName = username + return write(config) } func getConfigFilePath() (string, error) { @@ -68,7 +54,9 @@ func write(cfg Config) error { if err != nil { return err } - defer file.Close() + defer func(file *os.File) { + _ = file.Close() + }(file) encoder := json.NewEncoder(file) encoder.SetIndent("", " ") // For pretty printing diff --git a/internal/config/state.go b/internal/config/state.go new file mode 100644 index 0000000..26b4060 --- /dev/null +++ b/internal/config/state.go @@ -0,0 +1,5 @@ +package config + +type State struct { + Config *Config +} diff --git a/internal/handlers/loginHandler.go b/internal/handlers/loginHandler.go new file mode 100644 index 0000000..7a6b24c --- /dev/null +++ b/internal/handlers/loginHandler.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "errors" + "fmt" + "github.com/rdarius/boot-dev-blog-aggregator/internal/config" +) + +func HandlerLogin(s *config.State, cmd config.Command) error { + if len(cmd.Args) < 1 { + return errors.New("missing required argument USERNAME") + } + + err := s.Config.SetUser(cmd.Args[0]) + if err != nil { + return fmt.Errorf("failed to set current user: %w", err) + } + + fmt.Printf("User set to %s\n", s.Config.CurrentUserName) + + return nil +} diff --git a/main.go b/main.go index ee426eb..8e1e3a9 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,41 @@ package main import ( - "fmt" "github.com/rdarius/boot-dev-blog-aggregator/internal/config" + "github.com/rdarius/boot-dev-blog-aggregator/internal/handlers" "log" + "os" ) +var commands config.Commands + func main() { cfg, err := config.Read() if err != nil { log.Fatal(err) } - cfg.SetUser() - cfg2, err := config.Read() + state := config.State{ + Config: &cfg, + } + + commands = config.Commands{ + Commands: map[string]func(*config.State, config.Command) error{}, + } + + commands.Register("login", handlers.HandlerLogin) + + if len(os.Args) < 2 { + log.Fatal("usage: boot-dev-blog-aggregator [args...]") + return + } + + name := os.Args[1] + args := os.Args[2:] + + err = commands.Run(&state, config.Command{Name: name, Args: args}) if err != nil { log.Fatal(err) } - fmt.Printf("%v\n", cfg2) - }