Skip to content

Commit

Permalink
✨ Config cp command (#2)
Browse files Browse the repository at this point in the history
* 🏗 Add scaffolding

* wip

* 🚧 WIP

* Finished

* 🚧 WIP
  • Loading branch information
bdsoha authored Apr 21, 2024
1 parent a6ee1c1 commit f528a8b
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 7 deletions.
112 changes: 112 additions & 0 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 5 additions & 7 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/kloudkit/ws-cli/internals/net"
"github.com/kloudkit/ws-cli/internals/path"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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"),
)
},
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,6 +28,7 @@ func Execute() {

func init() {
rootCmd.AddCommand(
config.ConfigCmd,
feature.FeatureCmd,
fonts.FontsCmd,
get.GetCmd,
Expand Down
36 changes: 36 additions & 0 deletions internals/path/support.go
Original file line number Diff line number Diff line change
@@ -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
}
34 changes: 34 additions & 0 deletions internals/path/support_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}

0 comments on commit f528a8b

Please sign in to comment.