-
Notifications
You must be signed in to change notification settings - Fork 13
Extract internal/creconfig for CLI config paths #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
905d096
f6e39f5
25b6b21
cd8459d
dd4ec8c
aeefc78
c9cfaeb
8d9f2f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ Adds a template repository source | |
|
|
||
| ### Synopsis | ||
|
|
||
| Adds one or more template repository sources to ~/.cre/template.yaml. These repositories are used by cre init to discover available templates. | ||
| Adds one or more template repository sources to .cre/template.yaml. These repositories are used by cre init to discover available templates. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional to remove information that this file is stored by default in the hoemdir? Maybe we could write something like |
||
|
|
||
| ``` | ||
| cre templates add <owner/repo[@ref]>... [flags] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package creconfig | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| const Dir = ".cre" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it could be beneficial to use more explicit name. Or is it against Go rules? |
||
|
|
||
| // DirPath returns the absolute path to the CLI config directory. | ||
| func DirPath() (string, error) { | ||
| home, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("get home dir: %w", err) | ||
| } | ||
| return filepath.Join(home, Dir), nil | ||
| } | ||
|
|
||
| // EnsureDir creates the CLI config directory with 0700 permissions if missing. | ||
| func EnsureDir() (string, error) { | ||
| dir, err := DirPath() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if err := os.MkdirAll(dir, 0o700); err != nil { | ||
| return "", fmt.Errorf("create config dir: %w", err) | ||
| } | ||
| return dir, nil | ||
| } | ||
|
|
||
| // FilePath returns the absolute path to a file directly under the CLI config directory. | ||
| func FilePath(name string) (string, error) { | ||
| dir, err := DirPath() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Join(dir, name), nil | ||
| } | ||
|
|
||
| // FileRelPath returns a home-relative path for static help text and committed docs. | ||
| // It uses the host OS path separator (e.g. ".cre/template.yaml" or ".cre\template.yaml"). | ||
| func FileRelPath(name string) string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should name it |
||
| return filepath.Join(Dir, name) | ||
| } | ||
|
|
||
| // FilePathHint returns the absolute config file path for user-facing messages, | ||
| // or a home-relative path if the home directory cannot be resolved. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does it work if we can't resolve home dir? |
||
| func FilePathHint(name string) string { | ||
| if path, err := FilePath(name); err == nil { | ||
| return path | ||
| } | ||
| return FileRelPath(name) | ||
| } | ||
|
|
||
| // JoinPath returns an absolute path under the CLI config directory. | ||
| func JoinPath(elem ...string) (string, error) { | ||
| dir, err := DirPath() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Join(append([]string{dir}, elem...)...), nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we name the variable and function a bit more explicit? It looks quite generic now.