add status command

pull/25/head
Marcus Kok 2 years ago
parent 3f88e64d1a
commit 5473a2cf9b

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

@ -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)
}
}
}
}

@ -0,0 +1,44 @@
package test
import (
"bytes"
"os"
"path/filepath"
"testing"
"github.com/Marcusk19/dotctl/cmd"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestStatusCommand(t *testing.T) {
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