update init command, don't automatically search, just create config file for cli (#11)

pull/12/head
Marcus Kok 2 years ago committed by GitHub
parent d9a2b13470
commit f2ca64670a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -15,4 +15,4 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: go get . run: go get .
- name: Test with Go CLI - name: Test with Go CLI
run: go test -v ./test run: TESTING=true go test -v ./test

@ -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 ./...

@ -48,65 +48,31 @@ var initCommand = &cobra.Command {
Use: "init", Use: "init",
Short: "Copy configs to dotfile directory", Short: "Copy configs to dotfile directory",
Long: "Searches existing config directory for configs and then copies them to dotfile directory", Long: "Searches existing config directory for configs and then copies them to dotfile directory",
Run: func(cmd *cobra.Command, args []string) { Run: runInitCommand,
}
func runInitCommand(cmd *cobra.Command, args []string) {
fs := FileSystem fs := FileSystem
if(viper.Get("testing") == true && fs.Name() != "MemMapFS") { if(viper.Get("testing") == true && fs.Name() != "MemMapFS") {
log.Fatalf("wrong filesystem, got %s", fs.Name()) log.Fatalf("wrong filesystem, got %s", fs.Name())
} }
var rootpath string err := fs.MkdirAll(path.Join(DotfilePath, "bender"), 0755)
if len(args) <= 0 {
fmt.Fprintf(cmd.OutOrStdout(), "no path provided, assuming /usr/bin/\n")
rootpath = "/usr/bin/"
} else {
rootpath = args[0]
}
if rootpath[len(rootpath)-1:] != "/" {
log.Fatal("path needs trailing slash\n")
}
// 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 { if err != nil {
log.Fatalf("problem walking path %s\n", err) log.Fatalf("Unable to create dotfile structure: %s", error.Error(err))
return nil
}
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 { if err != nil {
log.Fatal(err) panic(fmt.Errorf("Unable to create config file %w", err))
} }
fmt.Fprintf(cmd.OutOrStdout(), "binaries found: \n =======================\n") if (viper.Get("testing") != "true"){
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) _, err = git.PlainInit(DotfilePath, false)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n") fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n")
},
} }

@ -4,6 +4,7 @@ Copyright © 2024 Marcus Kok
package cmd package cmd
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -38,11 +39,9 @@ var FileSystem afero.Fs
func init() { func init() {
// define flags and config sections // 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 // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
print("init of root\n")
defaultDotPath := os.Getenv("HOME") + "/.dotfiles/" defaultDotPath := os.Getenv("HOME") + "/.dotfiles/"
defaultConfPath := os.Getenv("HOME") + "/.config/" defaultConfPath := os.Getenv("HOME") + "/.config/"
RootCmd.PersistentFlags().StringVar( RootCmd.PersistentFlags().StringVar(
@ -63,22 +62,31 @@ func init() {
viper.BindEnv("testing") viper.BindEnv("testing")
viper.SetDefault("testing", false) 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() FileSystem = UseFilesystem()
} }
func UseFilesystem() afero.Fs { func UseFilesystem() afero.Fs {
testing := viper.Get("testing") testing := viper.Get("testing")
if(testing == true) { if(testing == "true") {
print("Using temporary testing filesystem\n")
return afero.NewMemMapFs() return afero.NewMemMapFs()
} else { } else {
return afero.NewOsFs() return afero.NewOsFs()
} }
} }
// TODO: this can probably be removed
func SetUpForTesting() afero.Fs { func SetUpForTesting() afero.Fs {
print("Setting up testing environment\n")
viper.Set("testing", true) viper.Set("testing", true)
fs := UseFilesystem() fs := UseFilesystem()

@ -11,23 +11,22 @@ import (
) )
func TestInitCommand(t *testing.T) { func TestInitCommand(t *testing.T) {
print("setting test var\n")
viper.Set("testing", true) viper.Set("testing", true)
fs := cmd.SetUpForTesting() fs := cmd.FileSystem
bender := cmd.RootCmd bender := cmd.RootCmd
actual := new(bytes.Buffer) actual := new(bytes.Buffer)
bender.SetOut(actual) bender.SetOut(actual)
bender.SetErr(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() bender.Execute()
homedir := "bender_test/" 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 { if err != nil {
t.Error(err.Error()) t.Error(err.Error())
} }

Loading…
Cancel
Save