From 6fc4dedac9c2e1f1f643d8e0f262b8b4502826f3 Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Fri, 26 Apr 2024 23:15:58 +0200 Subject: [PATCH] move artifact name detection to new package --- pkg/artifact/artifact.go | 15 +++++++++++++++ .../createHostedApplication.manual.go | 7 ++----- pkg/cmd/microservices/create/create.manual.go | 9 +++------ pkg/cmd/ui/plugins/create/create.manual.go | 9 +++------ 4 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 pkg/artifact/artifact.go diff --git a/pkg/artifact/artifact.go b/pkg/artifact/artifact.go new file mode 100644 index 000000000..26eea181d --- /dev/null +++ b/pkg/artifact/artifact.go @@ -0,0 +1,15 @@ +package artifact + +import ( + "path/filepath" + "regexp" +) + +// Parse the name from a given file by striping any version information from the file name +func ParseName(file string) string { + baseFileName := filepath.Base(file) + fileExt := filepath.Ext(baseFileName) + baseFileName = baseFileName[0 : len(baseFileName)-len(fileExt)] + versionRegex := regexp.MustCompile(`([_-]v?\d+\.\d+\.\d+(-SNAPSHOT)?)?$`) + return versionRegex.ReplaceAllString(baseFileName, "") +} diff --git a/pkg/cmd/applications/createhostedapplication/createHostedApplication.manual.go b/pkg/cmd/applications/createhostedapplication/createHostedApplication.manual.go index a014e5096..8752cded1 100644 --- a/pkg/cmd/applications/createhostedapplication/createHostedApplication.manual.go +++ b/pkg/cmd/applications/createhostedapplication/createHostedApplication.manual.go @@ -9,10 +9,10 @@ import ( "io" "os" "path/filepath" - "regexp" "strings" "github.com/MakeNowJust/heredoc/v2" + "github.com/reubenmiller/go-c8y-cli/v2/pkg/artifact" "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8ybinary" "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8yfetcher" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/subcommand" @@ -119,10 +119,7 @@ func (n *CmdCreateHostedApplication) getApplicationDetails(log *logger.Logger) ( app := Application{} // set default name to the file name - baseFileName := filepath.Base(n.file) - baseFileName = baseFileName[0 : len(baseFileName)-len(filepath.Ext(baseFileName))] - versionRegex := regexp.MustCompile(`(-v?\d+\.\d+\.\d+(-SNAPSHOT)?)?$`) - appNameFromFile := versionRegex.ReplaceAllString(baseFileName, "") + appNameFromFile := artifact.ParseName(n.file) // Set application properties diff --git a/pkg/cmd/microservices/create/create.manual.go b/pkg/cmd/microservices/create/create.manual.go index 083e20e10..9ba0f1e68 100644 --- a/pkg/cmd/microservices/create/create.manual.go +++ b/pkg/cmd/microservices/create/create.manual.go @@ -9,10 +9,10 @@ import ( "io" "os" "path/filepath" - "regexp" "strings" "github.com/MakeNowJust/heredoc/v2" + "github.com/reubenmiller/go-c8y-cli/v2/pkg/artifact" "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8ybinary" "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8yfetcher" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/subcommand" @@ -110,11 +110,8 @@ func (n *CmdCreate) getApplicationDetails(log *logger.Logger) (*Application, err app := Application{} // set default name to the file name - baseFileName := filepath.Base(n.file) - fileExt := filepath.Ext(baseFileName) - baseFileName = baseFileName[0 : len(baseFileName)-len(fileExt)] - versionRegex := regexp.MustCompile(`(-v?\d+\.\d+\.\d+(-SNAPSHOT)?)?$`) - appNameFromFile := versionRegex.ReplaceAllString(baseFileName, "") + fileExt := filepath.Ext(n.file) + appNameFromFile := artifact.ParseName(n.file) // Set application properties diff --git a/pkg/cmd/ui/plugins/create/create.manual.go b/pkg/cmd/ui/plugins/create/create.manual.go index 70203f235..4f1f74876 100644 --- a/pkg/cmd/ui/plugins/create/create.manual.go +++ b/pkg/cmd/ui/plugins/create/create.manual.go @@ -7,10 +7,10 @@ import ( "net/url" "os" "path/filepath" - "regexp" "strings" "github.com/MakeNowJust/heredoc/v2" + "github.com/reubenmiller/go-c8y-cli/v2/pkg/artifact" "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8ybinary" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/subcommand" "github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil" @@ -104,11 +104,8 @@ func NewCmdCreate(f *cmdutil.Factory) *CmdCreate { func (n *CmdCreate) getApplicationDetails(client *c8y.Client, log *logger.Logger) (*c8y.UIExtension, error) { // set default name to the file name - baseFileName := filepath.Base(n.file) - fileExt := filepath.Ext(baseFileName) - baseFileName = baseFileName[0 : len(baseFileName)-len(fileExt)] - versionRegex := regexp.MustCompile(`(-v?\d+\.\d+\.\d+(-SNAPSHOT)?)?$`) - appNameFromFile := versionRegex.ReplaceAllString(baseFileName, "") + appNameFromFile := artifact.ParseName(n.file) + fileExt := filepath.Ext(n.file) // Set application properties app, err := client.UIExtension.NewUIExtensionFromFile(n.file)