From f528a8b1a1b606e5ab0441daeb0f32410e85314b Mon Sep 17 00:00:00 2001 From: Dov Benyomin Sohacheski Date: Sun, 21 Apr 2024 08:21:25 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Config=20`cp`=20command=20(#2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🏗 Add scaffolding * wip * 🚧 WIP * Finished * 🚧 WIP --- cmd/config/config.go | 112 +++++++++++++++++++++++++++++++++ cmd/get/get.go | 12 ++-- cmd/root.go | 2 + internals/path/support.go | 36 +++++++++++ internals/path/support_test.go | 34 ++++++++++ 5 files changed, 189 insertions(+), 7 deletions(-) create mode 100644 cmd/config/config.go create mode 100644 internals/path/support.go create mode 100644 internals/path/support_test.go diff --git a/cmd/config/config.go b/cmd/config/config.go new file mode 100644 index 0000000..e7414b3 --- /dev/null +++ b/cmd/config/config.go @@ -0,0 +1,112 @@ +package config + +import ( + "fmt" + "io" + "os" + "path/filepath" + + "github.com/kloudkit/ws-cli/internals/path" + "github.com/spf13/cobra" +) + +type Config struct { + SourcePath string + OutputName string +} + +var configs = map[string]Config{ + "markdownlint": { + SourcePath: ".config/markdownlint/config", + OutputName: ".markdownlint.json", + }, + "ruff": { + SourcePath: ".config/ruff/ruff.toml", + OutputName: ".ruff.toml", + }, + "yamllint`": { + SourcePath: ".config/yamllint/config", + OutputName: ".yamllint", + }, +} + +var ConfigCmd = &cobra.Command{ + Use: "config", + Short: "Interact with workspace related configurations", +} + +var copyCmd = &cobra.Command{ + Use: "cp", + Short: "Copying workspace defined configurations to a project", +} + +func copy(source, dest string) error { + stats, err := os.Stat(source) + if err != nil { + return err + } + + if !stats.Mode().IsRegular() { + return fmt.Errorf("%s is not a regular file", source) + } + + sourceFile, err := os.Open(source) + if err != nil { + return err + } + defer sourceFile.Close() + + destFile, err := os.Create(dest) + if err != nil { + fmt.Println(err) + return err + } + defer destFile.Close() + + _, err = io.Copy(destFile, sourceFile) + + return err +} + +func createCommand(key string) *cobra.Command { + config := configs[key] + + return &cobra.Command{ + Use: key, + Short: fmt.Sprintf("Copy %s configuration to the project", key), + RunE: func(cmd *cobra.Command, args []string) error { + dest, _ := cmd.Flags().GetString("dest") + force, _ := cmd.Flags().GetBool("force") + + source := path.GetHomeDirectory(config.SourcePath) + + dest, _ = filepath.Abs(dest) + dest = path.AppendSegments(dest, config.OutputName) + + if !path.CanOverride(dest, force) { + return fmt.Errorf("the file [%s] already exists", dest) + } + + if err := copy(source, dest); err != nil { + return fmt.Errorf("the file [%s] could not be written", dest) + } + + fmt.Fprintf(cmd.OutOrStdout(), "Copied [%s] to [%s]\n", source, dest) + + return nil + }, + } +} + +func init() { + copyCmd.PersistentFlags().String("dest", ".", "Output directory") + copyCmd.PersistentFlags().BoolP("force", "f", false, "Force the overwriting of an existing file") + + copyCmd.AddCommand( + createCommand("markdownlint"), + createCommand("ruff"), + createCommand("yamllint"), + ) + + ConfigCmd.AddCommand(copyCmd) +} diff --git a/cmd/get/get.go b/cmd/get/get.go index 77a3163..4aca886 100644 --- a/cmd/get/get.go +++ b/cmd/get/get.go @@ -5,6 +5,7 @@ import ( "os" "github.com/kloudkit/ws-cli/internals/net" + "github.com/kloudkit/ws-cli/internals/path" "github.com/spf13/cobra" ) @@ -38,13 +39,10 @@ var settingsCmd = &cobra.Command{ return } - home, exists := os.LookupEnv("HOME") - - if !exists { - home = "/home/kloud" - } - - fmt.Fprintln(cmd.OutOrStdout(), home+"/.local/share/code-server/User/settings.json") + fmt.Fprintln( + cmd.OutOrStdout(), + path.GetHomeDirectory("/.local/share/code-server/User/settings.json"), + ) }, } diff --git a/cmd/root.go b/cmd/root.go index 6bd4820..bd277bd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/kloudkit/ws-cli/cmd/config" "github.com/kloudkit/ws-cli/cmd/feature" "github.com/kloudkit/ws-cli/cmd/fonts" "github.com/kloudkit/ws-cli/cmd/get" @@ -27,6 +28,7 @@ func Execute() { func init() { rootCmd.AddCommand( + config.ConfigCmd, feature.FeatureCmd, fonts.FontsCmd, get.GetCmd, diff --git a/internals/path/support.go b/internals/path/support.go new file mode 100644 index 0000000..53731b6 --- /dev/null +++ b/internals/path/support.go @@ -0,0 +1,36 @@ +package path + +import ( + "os" + "regexp" + "strings" +) + +func AppendSegments(root string, segments ...string) string { + if len(segments) != 0 { + root += "/" + strings.Join(segments, "/") + } + + re := regexp.MustCompile(`/+`) + root = re.ReplaceAllString(root, "/") + + return strings.TrimSuffix(root, "/") +} + +func GetHomeDirectory(segments ...string) string { + home, exists := os.LookupEnv("HOME") + + if !exists { + home = "/home/kloud" + } + + return AppendSegments(home, segments...) +} + +func CanOverride(path_ string, force_ bool) bool { + if _, err := os.Stat(path_); os.IsNotExist(err) && !force_ { + return false + } + + return true +} diff --git a/internals/path/support_test.go b/internals/path/support_test.go new file mode 100644 index 0000000..467c566 --- /dev/null +++ b/internals/path/support_test.go @@ -0,0 +1,34 @@ +package path_test + +import ( + "os" + "testing" + + "github.com/kloudkit/ws-cli/internals/path" + "gotest.tools/v3/assert" +) + + +func TestAppendSegments(t *testing.T) { + t.Run("AdditionalSegments", func(t *testing.T) { + assert.Equal(t, "/path", path.AppendSegments("/", "path")) + }) + + t.Run("NormalizeAdditionalSegments", func(t *testing.T) { + assert.Equal(t, "/home/sub/path", path.AppendSegments("/home/", "/sub", "path/")) + }) +} + +func TestGetHomeDirectory(t *testing.T) { + t.Run("WithEnv", func(t *testing.T) { + t.Setenv("HOME", "/app") + + assert.Equal(t, "/app", path.GetHomeDirectory()) + }) + + t.Run("WithoutEnv", func(t *testing.T) { + os.Unsetenv("HOME") + + assert.Equal(t, "/home/kloud", path.GetHomeDirectory()) + }) +}