add status command (#25)

* add status command

* new memmapfs on tests

* fixing all tests
pull/26/head
Marcus Kok 2 years ago committed by GitHub
parent 3f88e64d1a
commit 3a6284e45e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -26,8 +26,10 @@ var addCommand = &cobra.Command {
func runAddCommand(cmd *cobra.Command, args []string) { func runAddCommand(cmd *cobra.Command, args []string) {
fs := FileSystem fs := FileSystem
testing := viper.GetBool("testing")
if len(args) <= 0 { if len(args) <= 0 {
fmt.Println("ERROR: requires at least one argument") fmt.Println("ERROR: requires config path")
return return
} }
@ -38,9 +40,11 @@ func runAddCommand(cmd *cobra.Command, args []string) {
links := viper.GetStringMap("links") links := viper.GetStringMap("links")
links[name] = configSrc links[name] = configSrc
viper.Set("links", links) viper.Set("links", links)
err := viper.WriteConfig() if !testing {
if err != nil { err := viper.WriteConfig()
fmt.Printf("Problem updating dotctl config %s", err) if err != nil {
fmt.Printf("Problem updating dotctl config %s", err)
}
} }
dotfilePath := viper.Get("dotfile-path").(string) dotfilePath := viper.Get("dotfile-path").(string)
@ -52,7 +56,7 @@ func runAddCommand(cmd *cobra.Command, args []string) {
return return
} }
_, err = fs.Stat(dotfileDest) _, err := fs.Stat(dotfileDest)
if err == nil { if err == nil {
fmt.Printf("Looks like %s exists in current dotfile directory\n", dotfileDest) fmt.Printf("Looks like %s exists in current dotfile directory\n", dotfileDest)
fmt.Println("Do you want to overwrite it?") fmt.Println("Do you want to overwrite it?")

@ -55,6 +55,7 @@ func runInitCommand(cmd *cobra.Command, args []string) {
fs := FileSystem fs := FileSystem
// if user has passed a dotfile path flag need to add it to // if user has passed a dotfile path flag need to add it to
// viper's search path for a config file // viper's search path for a config file
testing := viper.GetBool("testing")
viper.AddConfigPath(filepath.Join(DotfilePath, "dotctl")) viper.AddConfigPath(filepath.Join(DotfilePath, "dotctl"))
if(viper.Get("testing") == true && fs.Name() != "MemMapFS") { if(viper.Get("testing") == true && fs.Name() != "MemMapFS") {
@ -71,12 +72,12 @@ func runInitCommand(cmd *cobra.Command, args []string) {
panic(fmt.Errorf("Unable to create config file %w", err)) panic(fmt.Errorf("Unable to create config file %w", err))
} }
err = viper.WriteConfig() if !testing {
if err != nil && viper.Get("testing") != true { err = viper.WriteConfig()
log.Fatalf("Unable to write config on init: %s\n", err) if err != nil && viper.Get("testing") != true {
} log.Fatalf("Unable to write config on init: %s\n", err)
}
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)

@ -0,0 +1,49 @@
package cmd
import (
"fmt"
"log"
"slices"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
RootCmd.AddCommand(statusCommand)
}
var statusCommand = &cobra.Command {
Use: "status",
Short: "View status of dotctl",
Long: "TODO: add longer description",
Run: runStatusCommand,
}
func runStatusCommand(cmd *cobra.Command, args[]string) {
fs := FileSystem
links := viper.GetStringMapString("links")
var ignoredDirs = []string{".git", "dotctl", ".gitignore"}
dotfiles, err := afero.ReadDir(fs, viper.GetString("dotfile-path"))
if err != nil {
log.Fatalf("Cannot read dotfile dir: %s\n", err)
}
fmt.Fprintln(cmd.OutOrStdout(), "Config directories currently in dotfile path:")
for _, dotfileDir := range(dotfiles) {
dirName := dotfileDir.Name()
if !slices.Contains(ignoredDirs, dirName) {
if links[dirName] != "" {
fmt.Fprintf(cmd.OutOrStdout(), "%s - %s\n", dirName, links[dirName])
} else {
fmt.Fprintln(cmd.OutOrStdout(), dirName)
}
}
}
}

@ -2,6 +2,7 @@ package test
import ( import (
"bytes" "bytes"
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -20,11 +21,11 @@ func TestInitCommand(t *testing.T) {
dotctl.SetOut(actual) dotctl.SetOut(actual)
dotctl.SetErr(actual) dotctl.SetErr(actual)
dotctl.SetArgs([]string{"init", "--dotfile-path=dotctl_test/dotfiles"}) dotctl.SetArgs([]string{"init"})
dotctl.Execute() dotctl.Execute()
homedir := "dotctl_test/" homedir := os.Getenv("HOME")
_, err := afero.ReadFile(fs, filepath.Join(homedir, "dotfiles/dotctl/config")) _, err := afero.ReadFile(fs, filepath.Join(homedir, "dotfiles/dotctl/config"))
if err != nil { if err != nil {

@ -8,14 +8,24 @@ import (
"testing" "testing"
"github.com/Marcusk19/dotctl/cmd" "github.com/Marcusk19/dotctl/cmd"
"github.com/spf13/afero"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestLinkCommand(t *testing.T) { func TestLinkCommand(t *testing.T) {
oldDotfilePath := viper.GetString("dotfile-path") viper.Set("testing", true)
setUpTesting() cmd.FileSystem = afero.NewMemMapFs()
fs := cmd.FileSystem
homedir := os.Getenv("HOME")
fs.MkdirAll(filepath.Join(homedir, "dotfiles/dotctl"), 0755)
links := map[string]string {
"someconfig": filepath.Join(homedir, ".config/someconfig"),
}
viper.Set("links", links)
dotctl := cmd.RootCmd dotctl := cmd.RootCmd
actual := new(bytes.Buffer) actual := new(bytes.Buffer)
@ -25,32 +35,11 @@ func TestLinkCommand(t *testing.T) {
dotctl.Execute() dotctl.Execute()
homedir := os.Getenv("HOME")
someconfig := filepath.Join(homedir, ".config/someconfig/") someconfig := filepath.Join(homedir, ".config/someconfig/")
somedot := filepath.Join(homedir, "dotfiles/someconfig/") somedot := filepath.Join(homedir, "dotfiles/someconfig/")
expected := fmt.Sprintf("%s,%s", someconfig, somedot) expected := fmt.Sprintf("%s,%s", someconfig, somedot)
assert.Equal(t, expected, actual.String(), "actual differs from expected") assert.Equal(t, expected, actual.String(), "actual differs from expected")
tearDownTesting(oldDotfilePath)
} }
func setUpTesting() {
viper.Set("testing", true)
fs := cmd.FileSystem
homedir := os.Getenv("HOME")
fakeLinks := map[string]string {"someconfig": filepath.Join(homedir, ".config/someconfig")}
viper.Set("links", fakeLinks)
fs.MkdirAll(filepath.Join(homedir, "dotfiles/dotctl"), 0755)
fs.Create(filepath.Join(homedir, "dotfiles/dotctl/config"))
viper.Set("dotfile-path", filepath.Join(homedir, "dotfiles"))
viper.Set("someconfig", filepath.Join(homedir, ".config/someconfig/"))
}
func tearDownTesting(oldDotfilePath string) {
viper.Set("dotfile-path", oldDotfilePath)
viper.WriteConfig()
}

@ -0,0 +1,46 @@
package test
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/Marcusk19/dotctl/cmd"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestStatusCommand(t *testing.T) {
cmd.FileSystem = afero.NewMemMapFs()
viper.Set("testing", true)
fs := cmd.FileSystem
homedir := os.Getenv("HOME")
fs.MkdirAll(filepath.Join(homedir, "dotfiles/dotctl"), 0755)
fs.MkdirAll(filepath.Join(homedir, "dotfiles/someconfig"), 0755)
fs.MkdirAll(filepath.Join(homedir, "dotfiles/somelinkedconfig"), 0755)
var links = map[string]string {
"somelinkedconfig": "configpath",
}
viper.Set("links", links)
dotctl := cmd.RootCmd
actual := new(bytes.Buffer)
dotctl.SetOut(actual)
dotctl.SetErr(actual)
dotctl.SetArgs([]string{"status"})
dotctl.Execute()
expected := "Config directories currently in dotfile path:\n" +
"someconfig\nsomelinkedconfig - configpath\n"
assert.Equal(t, expected, actual.String(), "actual differs from expected")
}
Loading…
Cancel
Save