Skip to content
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 0 additions & 28 deletions .github/workflows/go.yml

This file was deleted.

72 changes: 19 additions & 53 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
18 changes: 13 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright © 2024 Marcus Kok
package cmd

import (
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -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(
Expand All @@ -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()

Expand Down
7 changes: 3 additions & 4 deletions test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down