From f2ca64670aada49bffac3adf29ed6db66c0dd7e7 Mon Sep 17 00:00:00 2001 From: Marcus Kok <47163063+Marcusk19@users.noreply.github.com> Date: Wed, 20 Mar 2024 14:13:19 -0400 Subject: [PATCH] update init command, don't automatically search, just create config file for cli (#11) --- .github/workflows/ci.yml | 2 +- .github/workflows/go.yml | 28 ---------------- cmd/init.go | 72 +++++++++++----------------------------- cmd/root.go | 18 +++++++--- test/init_test.go | 7 ++-- 5 files changed, 36 insertions(+), 91 deletions(-) delete mode 100644 .github/workflows/go.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c78f2c..4ffe45e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,4 +15,4 @@ jobs: - name: Install dependencies run: go get . - name: Test with Go CLI - run: go test -v ./test + run: TESTING=true go test -v ./test diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index 63af703..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,28 +0,0 @@ -# This workflow will build a golang project -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go - -name: Go - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -jobs: - - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: '1.22.x' - - - name: Build - run: go build -v ./... - - - name: Test - run: go test -v ./... diff --git a/cmd/init.go b/cmd/init.go index 02aa11e..b48550f 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -48,65 +48,31 @@ var initCommand = &cobra.Command { Use: "init", Short: "Copy configs to dotfile directory", Long: "Searches existing config directory for configs and then copies them to dotfile directory", - Run: func(cmd *cobra.Command, args []string) { - - fs := FileSystem - - if(viper.Get("testing") == true && fs.Name() != "MemMapFS") { - log.Fatalf("wrong filesystem, got %s", fs.Name()) - } + Run: runInitCommand, +} - var rootpath string - if len(args) <= 0 { - fmt.Fprintf(cmd.OutOrStdout(), "no path provided, assuming /usr/bin/\n") - rootpath = "/usr/bin/" - } else { - rootpath = args[0] - } +func runInitCommand(cmd *cobra.Command, args []string) { + fs := FileSystem - if rootpath[len(rootpath)-1:] != "/" { - log.Fatal("path needs trailing slash\n") - } + if(viper.Get("testing") == true && fs.Name() != "MemMapFS") { + log.Fatalf("wrong filesystem, got %s", fs.Name()) + } - // TODO make a configurable list of binaries we want to look for - var programs []string - var acceptedprograms [3] string - acceptedprograms[0] = "nvim" - acceptedprograms[1] = "tmux" - acceptedprograms[2] = "alacritty" - - err := afero.Walk(fs, rootpath, func(path string, info os.FileInfo, err error) error { - if err != nil { - log.Fatalf("problem walking path %s\n", err) - return nil - } + err := fs.MkdirAll(path.Join(DotfilePath, "bender"), 0755) + if err != nil { + log.Fatalf("Unable to create dotfile structure: %s", error.Error(err)) + } - for _, acceptedprogram := range(acceptedprograms) { - if path == rootpath + acceptedprogram { - programs = append(programs, path[len(rootpath):]) - } - } - return nil - }) + _, err = fs.Create(path.Join(DotfilePath, "bender/bender.yml")) + if err != nil { + panic(fmt.Errorf("Unable to create config file %w", err)) + } + if (viper.Get("testing") != "true"){ + _, err = git.PlainInit(DotfilePath, false) if err != nil { log.Fatal(err) } - - fmt.Fprintf(cmd.OutOrStdout(), "binaries found: \n =======================\n") - for _, program := range(programs) { - fmt.Fprintf(cmd.OutOrStdout(), program + "\n" ) - } - - createDotfileStructure(programs, fs) - copyExistingConfigs(programs, fs) - - if (viper.Get("testing") != true){ - _, err = git.PlainInit(DotfilePath, false) - if err != nil { - log.Fatal(err) - } - } - fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n") - }, + } + fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n") } diff --git a/cmd/root.go b/cmd/root.go index 8878537..f805061 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ Copyright © 2024 Marcus Kok package cmd import ( + "fmt" "os" "path/filepath" @@ -38,11 +39,9 @@ var FileSystem afero.Fs func init() { // define flags and config sections - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bender.yaml)") // Cobra also supports local flags, which will only run // when this action is called directly. - print("init of root\n") defaultDotPath := os.Getenv("HOME") + "/.dotfiles/" defaultConfPath := os.Getenv("HOME") + "/.config/" RootCmd.PersistentFlags().StringVar( @@ -63,22 +62,31 @@ func init() { viper.BindEnv("testing") viper.SetDefault("testing", false) + viper.SetConfigName("bender.yml") + viper.SetConfigType("yaml") + viper.AddConfigPath(filepath.Join(defaultDotPath, "bender")) + viper.AddConfigPath("./bender") + + err := viper.ReadInConfig() + if err != nil { + fmt.Println("No config detected. You can generate one by using 'bender init'") + } + FileSystem = UseFilesystem() } func UseFilesystem() afero.Fs { testing := viper.Get("testing") - if(testing == true) { - print("Using temporary testing filesystem\n") + if(testing == "true") { return afero.NewMemMapFs() } else { return afero.NewOsFs() } } +// TODO: this can probably be removed func SetUpForTesting() afero.Fs { - print("Setting up testing environment\n") viper.Set("testing", true) fs := UseFilesystem() diff --git a/test/init_test.go b/test/init_test.go index 26f8f1f..1c1e481 100644 --- a/test/init_test.go +++ b/test/init_test.go @@ -11,23 +11,22 @@ import ( ) func TestInitCommand(t *testing.T) { - print("setting test var\n") viper.Set("testing", true) - fs := cmd.SetUpForTesting() + fs := cmd.FileSystem bender := cmd.RootCmd actual := new(bytes.Buffer) bender.SetOut(actual) bender.SetErr(actual) - bender.SetArgs([]string{"init", "bin/", "--dotfile-path=bender_test/.dotfiles", "--config-path=bender_test/.config"}) + bender.SetArgs([]string{"init", "--dotfile-path=bender_test/.dotfiles"}) bender.Execute() homedir := "bender_test/" - _, err := afero.ReadFile(fs, filepath.Join(homedir, ".dotfiles/alacritty/alacritty.conf")) + _, err := afero.ReadFile(fs, filepath.Join(homedir, ".dotfiles/bender/bender.yml")) if err != nil { t.Error(err.Error()) }