From 328a0a9164d99598a70aca2fbce6f8ce05bd07bc Mon Sep 17 00:00:00 2001 From: AlexNg Date: Fri, 11 Oct 2024 22:40:34 +0800 Subject: [PATCH 1/8] typo: Stirng -> String Signed-off-by: AlexNg --- internal/types/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/types/cli.go b/internal/types/cli.go index 983ab067..5de0c46b 100644 --- a/internal/types/cli.go +++ b/internal/types/cli.go @@ -1,6 +1,6 @@ package types -// The for CLI options +// The for CLI options const ( PATH string = "" REPO string = "" From 8207b9908ff2a112ab8f1c997966f651f869dc67 Mon Sep 17 00:00:00 2001 From: AlexNg Date: Fri, 11 Oct 2024 23:49:54 +0800 Subject: [PATCH 2/8] feat: Add source flag and deprecate repo flag Ref #161 Signed-off-by: AlexNg --- cmd/commands/new.go | 4 ++++ cmd/options/new.go | 16 +++++++++++++--- internal/types/cli.go | 9 +++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cmd/commands/new.go b/cmd/commands/new.go index 694b7ee5..235b9dc4 100644 --- a/cmd/commands/new.go +++ b/cmd/commands/new.go @@ -248,12 +248,16 @@ func init() { func AddNewCmdFlags(cmd *cobra.Command) { cmd.Flags().VarP(&options.NewOpts.Repo, "repo", "r", "source repository to template from") + cmd.Flags().VarP(&options.NewOpts.Source, "source", "s", "source repository to template from") cmd.Flags().VarP(&options.NewOpts.Branch, "branch", "b", "branch to clone from") cmd.Flags().VarP(&options.NewOpts.Directory, "directory", "D", "directory where config is located") cmd.Flags().VarP(&options.NewOpts.Name, "name", "n", "name of the project") cmd.Flags().VarP(&options.NewOpts.License, "license", "l", "license to use for the project") cmd.Flags().VarP(&options.NewOpts.Style, "style", "S", "which style to use") cmd.Flags().BoolVarP(&options.NewOpts.NoGit, "no-git", "G", options.NewOpts.NoGit, "whether to not initialize git") + + cmd.Flags().MarkDeprecated("repo", "Please use --source instead.") + cmd.MarkFlagsMutuallyExclusive("source", "repo") } func WriteFiles(tmpRoot, projectRoot string, paths []string, licenseText string, tmpl map[string]any, licenseTmpl map[string]string) error { diff --git a/cmd/options/new.go b/cmd/options/new.go index 73edeb5a..4ed665af 100644 --- a/cmd/options/new.go +++ b/cmd/options/new.go @@ -16,6 +16,7 @@ const defaultRepo = "https://github.com/caffeine-addictt/waku.git" // The options for the new command var NewOpts = NewOptions{ Repo: *types.NewValueGuard("", cmdOpt, types.REPO), + Source: *types.NewValueGuard("", cmdOpt, types.REPO), Branch: *types.NewValueGuard("", cmdOpt, types.BRANCH), Directory: *types.NewValueGuard("", cmdOpt, types.PATH), Name: *types.NewValueGuard("", cmdOpt, types.STRING), @@ -29,6 +30,10 @@ type NewOptions struct { // Should be this repository by default Repo types.ValueGuard[string] + // The repository Url or local path to use + // Should be this repository by default + Source types.ValueGuard[string] + // The branch to use Branch types.ValueGuard[string] @@ -54,9 +59,14 @@ func cmdOpt(v string) (string, error) { // TO be invoked before a command is ran func (o *NewOptions) Validate() error { - switch o.Repo.Value() { + // Since both flags are mutually exclusive + if err := o.Source.Set(o.Source.Value() + o.Repo.Value()); err != nil { + return err + } + + switch o.Source.Value() { case "": - if err := o.Repo.Set(defaultRepo); err != nil { + if err := o.Source.Set(defaultRepo); err != nil { return err } if err := o.Directory.Set("template"); err != nil { @@ -97,7 +107,7 @@ func (o *NewOptions) CloneRepo() (string, error) { opts := git.CloneOptions{ Depth: 1, Branch: o.Branch.Value(), - Url: utils.EscapeTermString(o.Repo.Value()), + Url: utils.EscapeTermString(o.Source.Value()), ClonePath: utils.EscapeTermString(tmpDirPath), } diff --git a/internal/types/cli.go b/internal/types/cli.go index 5de0c46b..b84c3a4c 100644 --- a/internal/types/cli.go +++ b/internal/types/cli.go @@ -2,8 +2,9 @@ package types // The for CLI options const ( - PATH string = "" - REPO string = "" - BRANCH string = "" - STRING string = "" + PATH string = "" + REPO string = "" + BRANCH string = "" + STRING string = "" + REPO_OR_PATH = "" ) From 99a33b1581bcf6b4797ee4344c491cbc94cbb5f5 Mon Sep 17 00:00:00 2001 From: AlexNg Date: Fri, 11 Oct 2024 23:50:27 +0800 Subject: [PATCH 3/8] fix: Fetching broken when passing source flag Signed-off-by: AlexNg --- internal/license/store.go | 14 +++++++++++++- internal/license/types.go | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/license/store.go b/internal/license/store.go index 3078103f..e2cf6155 100644 --- a/internal/license/store.go +++ b/internal/license/store.go @@ -7,6 +7,7 @@ import ( "github.com/caffeine-addictt/waku/cmd/options" "github.com/caffeine-addictt/waku/internal/log" + "github.com/caffeine-addictt/waku/pkg/version" "github.com/goccy/go-json" ) @@ -19,6 +20,17 @@ const ( // need to hit the endpoint once per session. var Licenses *[]License +func GetLicenseFetchUrl() string { + var url string + if options.NewOpts.Branch.Value() != "" { + url = fmt.Sprintf(BASE_URL, options.NewOpts.Branch.Value()) + } else { + url = fmt.Sprintf(BASE_URL, "v"+version.Version) + } + url += LICENSE_LIST + return url +} + // GetLicenses returns the list of licenses from the GitHub API // or returns the cached list if it exists. func GetLicenses() (*[]License, error) { @@ -26,7 +38,7 @@ func GetLicenses() (*[]License, error) { return Licenses, nil } - url := fmt.Sprintf(BASE_URL, options.NewOpts.Branch.Value()) + LICENSE_LIST + url := GetLicenseFetchUrl() log.Infof("Fetching licenses from %s...\n", url) req, err := http.NewRequest(http.MethodGet, url, http.NoBody) if err != nil { diff --git a/internal/license/types.go b/internal/license/types.go index bd92ddd4..8e3d9155 100644 --- a/internal/license/types.go +++ b/internal/license/types.go @@ -28,7 +28,7 @@ type License struct { } func (license *License) GetLicenseText() (string, error) { - req, err := http.NewRequest(http.MethodGet, BASE_URL+license.Filename, http.NoBody) + req, err := http.NewRequest(http.MethodGet, GetLicenseFetchUrl()+license.Filename, http.NoBody) if err != nil { return "", err } From 22e409ec2201b5c6ac436902ddd839fed75f18d2 Mon Sep 17 00:00:00 2001 From: AlexNg Date: Fri, 11 Oct 2024 23:51:08 +0800 Subject: [PATCH 4/8] feat: Add checking urls to git Signed-off-by: AlexNg --- internal/git/check.go | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 internal/git/check.go diff --git a/internal/git/check.go b/internal/git/check.go new file mode 100644 index 00000000..6ff58010 --- /dev/null +++ b/internal/git/check.go @@ -0,0 +1,65 @@ +package git + +import ( + "net/url" + "os" + + "github.com/caffeine-addictt/waku/internal/log" +) + +type UrlType string + +const ( + GitUrlType UrlType = "git" + PathUrlType UrlType = "path" + BadUrlType UrlType = "bad" +) + +var validSchemas [4]string = [4]string{"http", "https", "git", "ssh"} + +// CheckUrl checks if a given string s is +// a valid git or path url +func CheckUrl(s string) UrlType { + if IsGitUrl(s) { + return GitUrlType + } + + if IsPathUrl(s) { + return PathUrlType + } + + return BadUrlType +} + +// IsPathUrl checks if a given string s is +// a valid fs path +func IsPathUrl(s string) bool { + log.Debugf("checking if %s is a valid path\n", s) + _, err := os.Stat(s) + v := !os.IsNotExist(err) + + if !v { + log.Debugf("%s is not a valid path", s) + } + + return v +} + +// IsGitUrl checks if a given string s is +// a valid Git url +func IsGitUrl(s string) bool { + log.Debugf("checking if %s is a valid git url\n", s) + parsedUrl, err := url.Parse(s) + if err != nil { + log.Debugf("%s is not a valid url\n", s) + return false + } + + for _, schema := range validSchemas { + if parsedUrl.Scheme == schema { + return true + } + } + + return false +} From 076245c2665e68c05ae4fa133b49b3b146cf7755 Mon Sep 17 00:00:00 2001 From: AlexNg Date: Fri, 11 Oct 2024 23:51:33 +0800 Subject: [PATCH 5/8] feat: Implement source flag Signed-off-by: AlexNg --- cmd/commands/new.go | 11 ++--------- cmd/options/new.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/cmd/commands/new.go b/cmd/commands/new.go index 235b9dc4..afcc83b8 100644 --- a/cmd/commands/new.go +++ b/cmd/commands/new.go @@ -71,17 +71,10 @@ var NewCmd = &cobra.Command{ }) // Clone repo - tmpDir, err := options.NewOpts.CloneRepo() + tmpDir, err := options.NewOpts.GetSource() if err != nil { - return errors.NewWakuErrorf("could not clone git repo: %s", err) + return errors.ToWakuError(err) } - cleanup.Schedule(func() error { - log.Debugf("removing tmp dir: %s\n", tmpDir) - if err := os.RemoveAll(tmpDir); err != nil { - return errors.NewWakuErrorf("failed to cleanup tmp dir: %v", err) - } - return nil - }) // Resolve dir rootDir := tmpDir diff --git a/cmd/options/new.go b/cmd/options/new.go index 4ed665af..ad9f8bc3 100644 --- a/cmd/options/new.go +++ b/cmd/options/new.go @@ -4,6 +4,8 @@ import ( "errors" "os" + "github.com/caffeine-addictt/waku/cmd/cleanup" + e "github.com/caffeine-addictt/waku/internal/errors" "github.com/caffeine-addictt/waku/internal/git" "github.com/caffeine-addictt/waku/internal/log" "github.com/caffeine-addictt/waku/internal/types" @@ -93,6 +95,35 @@ func (o *NewOptions) Validate() error { return nil } +// GetSource returns the source directory path +// that is either cloned with Git or is local. +// +// If it is a Git cloned path, it will be cleaned +func (o *NewOptions) GetSource() (string, error) { + switch git.CheckUrl(o.Source.Value()) { + case git.GitUrlType: + s, err := o.CloneRepo() + if err != nil { + return s, e.NewWakuErrorf("could not clone repo: %v", err) + } + + cleanup.Schedule(func() error { + log.Debugf("removing tmp dir: %s\n", s) + if err := os.RemoveAll(s); err != nil { + return e.NewWakuErrorf("failed to cleanup tmp dir: %v", err) + } + return nil + }) + + return s, err + + case git.PathUrlType: + return o.Source.Value(), nil + } + + return "", e.NewWakuErrorf("invalid source URL or path: %s", o.Source.Value()) +} + // To clone the repository func (o *NewOptions) CloneRepo() (string, error) { log.Debugln("creating tmp dir") From 5f36f59bb5c8e7c375d6b557de339799139f9364 Mon Sep 17 00:00:00 2001 From: AlexNg Date: Fri, 11 Oct 2024 23:52:07 +0800 Subject: [PATCH 6/8] chore: bump 0.6.0 -> 0.7.0 Signed-off-by: AlexNg --- pkg/version/version.go | 2 +- www/docs/blog/posts/2024-10-08-waku-v0.6.0.md | 2 +- www/docs/configuration/introduction.md | 4 ++-- www/docs/install.md | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/version/version.go b/pkg/version/version.go index 5c0788fb..fb91900e 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,4 +1,4 @@ package version // The current app version -const Version = "0.6.0" +const Version = "0.7.0" diff --git a/www/docs/blog/posts/2024-10-08-waku-v0.6.0.md b/www/docs/blog/posts/2024-10-08-waku-v0.6.0.md index 70bda197..f67155b5 100644 --- a/www/docs/blog/posts/2024-10-08-waku-v0.6.0.md +++ b/www/docs/blog/posts/2024-10-08-waku-v0.6.0.md @@ -25,7 +25,7 @@ Waku has a Discord server. [Join us!](https://discord.gg/NcRFkVTcaw) ## Download You can [install](../../install.md) or upgrade Waku with your favorite package manager, -or see the full release notes [here](https://github.com/caffeine-addictt/waku/releases/tag/v0.6.0). +or see the full release notes [here](https://github.com/caffeine-addictt/waku/releases/tag/v0.7.0). ## Helping out diff --git a/www/docs/configuration/introduction.md b/www/docs/configuration/introduction.md index 9f17414b..aa613a96 100644 --- a/www/docs/configuration/introduction.md +++ b/www/docs/configuration/introduction.md @@ -20,10 +20,10 @@ for better editor support. https://waku.ngjx.org/static/schema.json ``` -Or you can pin a specific version like `v0.6.0`: +Or you can pin a specific version like `v0.7.0`: ```text -https://raw.githubusercontent.com/caffeine-addictt/waku/v0.6.0/www/docs/static/schema.json +https://raw.githubusercontent.com/caffeine-addictt/waku/v0.7.0/www/docs/static/schema.json ``` Simply add the `$schema` property to your `configuration` file: diff --git a/www/docs/install.md b/www/docs/install.md index 901be985..11275c22 100644 --- a/www/docs/install.md +++ b/www/docs/install.md @@ -102,17 +102,17 @@ All artifacts are checksummed, and the checksum file is signed with [cosign][]. and `checksums.txt.sig` files from the [releases][] page. ```sh - curl -O 'https://github.com/caffeine-addictt/waku/releases/download/v0.6.0/checksums.txt' + curl -O 'https://github.com/caffeine-addictt/waku/releases/download/v0.7.0/checksums.txt' ``` 1. Verify checksums signature: ```bash cosign verify-blob \ - --certificate-identity 'https://github.com/caffeine-addictt/waku/.github/workflows/release.yml@refs/tags/v0.6.0' \ + --certificate-identity 'https://github.com/caffeine-addictt/waku/.github/workflows/release.yml@refs/tags/v0.7.0' \ --certificate-oidc-issuer 'https://token.actions.githubusercontent.com' \ - --cert 'https://github.com/caffeine-addictt/waku/releases/download/v0.6.0/checksums.txt.pem' \ - --signature 'https://github.com/caffeine-addictt/waku/releases/download/v0.6.0/checksums.txt.sig' \ + --cert 'https://github.com/caffeine-addictt/waku/releases/download/v0.7.0/checksums.txt.pem' \ + --signature 'https://github.com/caffeine-addictt/waku/releases/download/v0.7.0/checksums.txt.sig' \ ./checksums.txt ``` @@ -130,7 +130,7 @@ Verify the signature: ```sh cosign verify \ - --certificate-identity 'https://github.com/caffeine-addictt/waku/.github/workflows/release.yml@refs/tags/v0.6.0' \ + --certificate-identity 'https://github.com/caffeine-addictt/waku/.github/workflows/release.yml@refs/tags/v0.7.0' \ --certificate-oidc-issuer 'https://token.actions.githubusercontent.com' \ caffeinec/waku ``` From 0550cb61f94c8f21446148d1a4b5c7126aa8a37d Mon Sep 17 00:00:00 2001 From: AlexNg Date: Sat, 12 Oct 2024 00:00:01 +0800 Subject: [PATCH 7/8] docs: Add repo flag deprecation notice Signed-off-by: AlexNg --- www/docs/deprecations.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/www/docs/deprecations.md b/www/docs/deprecations.md index 2554c252..6b069654 100644 --- a/www/docs/deprecations.md +++ b/www/docs/deprecations.md @@ -2,6 +2,19 @@ This page lists all deprecation notices in Waku. +Active deprecations will be removed in the next major version, +please update your code accordingly. + +## Active notices + +### waku new `--repo` flag + +> since v0.7 + +This flag was deprecated in favor of the `--source` flag which works +like `--repo` but it also accepts local directory paths. Switching +to `--source` is __NOT BREAKING__. + ## Removed in v0 Nothing so far. :partying_face: From c43354efeb1e043d160476a81067be0e2dac1694 Mon Sep 17 00:00:00 2001 From: AlexNg Date: Sat, 12 Oct 2024 00:03:21 +0800 Subject: [PATCH 8/8] feat: check for markDeprecated err Signed-off-by: AlexNg --- cmd/commands/new.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/commands/new.go b/cmd/commands/new.go index afcc83b8..a7be8bbe 100644 --- a/cmd/commands/new.go +++ b/cmd/commands/new.go @@ -249,7 +249,9 @@ func AddNewCmdFlags(cmd *cobra.Command) { cmd.Flags().VarP(&options.NewOpts.Style, "style", "S", "which style to use") cmd.Flags().BoolVarP(&options.NewOpts.NoGit, "no-git", "G", options.NewOpts.NoGit, "whether to not initialize git") - cmd.Flags().MarkDeprecated("repo", "Please use --source instead.") + if err := cmd.Flags().MarkDeprecated("repo", "Please use --source instead."); err != nil { + panic(err) + } cmd.MarkFlagsMutuallyExclusive("source", "repo") }