mirror of https://github.com/Marcusk19/dotctl
add status command (#25)
* add status command * new memmapfs on tests * fixing all testspull/26/head
parent
3f88e64d1a
commit
3a6284e45e
@ -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,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…
Reference in New Issue