Skip to content

Commit

Permalink
move artifact name detection to new package
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenmiller committed Apr 26, 2024
1 parent 3b96c2f commit 6fc4ded
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
15 changes: 15 additions & 0 deletions pkg/artifact/artifact.go
Original file line number Diff line number Diff line change
@@ -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, "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions pkg/cmd/microservices/create/create.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions pkg/cmd/ui/plugins/create/create.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6fc4ded

Please sign in to comment.