Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code and remove additional structs where possible #10

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ build-release:

.PHONY: gofmt
gofmt:
gofmt -l -s -w ./cmd/config
gofmt -l -s -w ./cmd/config ./pkg

.PHONY: mod
mod:
Expand Down
26 changes: 3 additions & 23 deletions cmd/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"fmt"
"os"

"github.com/dmhdeveloper/config/command"
"github.com/dmhdeveloper/config/configs"
"github.com/dmhdeveloper/config/logger"
"github.com/dmhdeveloper/config/pkg/cli"
)

var (
Expand All @@ -15,29 +13,11 @@ var (
Version string
)

var log = logger.FmtLogger{}

func main() {
if len(os.Args) == 1 {
log.Println(fmt.Sprint("Config version: ", Version, ", Build time: ", BuildTime, ", Git hash: ", GitHash))
fmt.Println(fmt.Sprint("Config version: ", Version, ", Build time: ", BuildTime, ", Git hash: ", GitHash))
os.Exit(0)
}

conf, err := configs.LoadConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

switch os.Args[1] {
case "init":
command := command.NewInitCmd(log)
os.Exit(command.Run(os.Args[2:]...))
case "-h":
helpMessage := command.RunHelp(command.InitCmd{}.Help, command.GitCmd{}.Help)
log.Println(helpMessage)
default:
command := command.NewGitCmd(conf.GitDir, conf.WorkTree, os.Stdout)
os.Exit(command.Run(os.Args[1:]...))
}
os.Exit(cli.RunConfig(os.Args))
}
23 changes: 0 additions & 23 deletions command/command.go

This file was deleted.

71 changes: 0 additions & 71 deletions command/git.go

This file was deleted.

128 changes: 0 additions & 128 deletions command/init.go

This file was deleted.

13 changes: 0 additions & 13 deletions logger/logger.go

This file was deleted.

34 changes: 34 additions & 0 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cli

import (
"fmt"

"github.com/dmhdeveloper/config/pkg/command"
)

const helpMessage = ""

func RunConfig(
args []string,
) int {
// No arguments passed, return help message
if len(args) == 0 {
fmt.Println(helpMessage)
return 0
}

switch args[1] {
case "help", "-h", "--help":
fmt.Println(helpMessage)
case "init":
return command.RunInit(args[2:])
case "git":
return command.RunGit(args[2:])
default:
fmt.Printf("Uknown command: %s\n\n", args[1])
fmt.Println(helpMessage)
}

// No error
return 0
}
40 changes: 40 additions & 0 deletions pkg/command/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package command

import (
"fmt"
"os"
"os/exec"

"github.com/dmhdeveloper/config/pkg/configs"
)

func RunGit(
args []string,
) int {
conf, err := configs.LoadGitConfig()
if err != nil {
fmt.Println(err)
return 1
}

defaults := []string{"--git-dir", conf.GitDir, "--work-tree", conf.WorkTree}
defaults = append(defaults, args...)
cmd := exec.Command("git", defaults...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr

err = cmd.Start()
if err != nil {
fmt.Println(err)
return 1
}

err = cmd.Wait()
if err != nil {
fmt.Println(err)
return 1
}

return 0
}
Loading
Loading