-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🏗 Add scaffolding * wip * 🚧 WIP * Finished * 🚧 WIP
- Loading branch information
Showing
5 changed files
with
189 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} |