This commit is contained in:
2025-03-11 08:36:29 +02:00
parent d1163ec629
commit f23f3fbf53
6 changed files with 81 additions and 23 deletions

View File

@@ -0,0 +1,6 @@
package config
type Command struct {
Name string
Args []string
}

View File

@@ -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)
}

View File

@@ -3,7 +3,6 @@ package config
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"os/user"
) )
const configFileName = ".gatorconfig.json" const configFileName = ".gatorconfig.json"
@@ -31,22 +30,9 @@ func Read() (Config, error) {
} }
func (config Config) SetUser() error { func (config Config) SetUser(username string) error {
usr, err := user.Current() config.CurrentUserName = username
return write(config)
if err != nil {
return err
}
config.CurrentUserName = usr.Username
err = write(config)
if err != nil {
return err
}
return nil
} }
func getConfigFilePath() (string, error) { func getConfigFilePath() (string, error) {
@@ -68,7 +54,9 @@ func write(cfg Config) error {
if err != nil { if err != nil {
return err return err
} }
defer file.Close() defer func(file *os.File) {
_ = file.Close()
}(file)
encoder := json.NewEncoder(file) encoder := json.NewEncoder(file)
encoder.SetIndent("", " ") // For pretty printing encoder.SetIndent("", " ") // For pretty printing

5
internal/config/state.go Normal file
View File

@@ -0,0 +1,5 @@
package config
type State struct {
Config *Config
}

View File

@@ -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
}

28
main.go
View File

@@ -1,23 +1,41 @@
package main package main
import ( import (
"fmt"
"github.com/rdarius/boot-dev-blog-aggregator/internal/config" "github.com/rdarius/boot-dev-blog-aggregator/internal/config"
"github.com/rdarius/boot-dev-blog-aggregator/internal/handlers"
"log" "log"
"os"
) )
var commands config.Commands
func main() { func main() {
cfg, err := config.Read() cfg, err := config.Read()
if err != nil { if err != nil {
log.Fatal(err) 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 <command> [args...]")
return
}
name := os.Args[1]
args := os.Args[2:]
err = commands.Run(&state, config.Command{Name: name, Args: args})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("%v\n", cfg2)
} }