From c2d89ccf2b8eb83d5f657d227e398981c2089ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Svantesson?= Date: Fri, 10 Jan 2025 20:11:03 +0100 Subject: [PATCH] fix: remove some dead code stop using draft library unnecessarily add version command related to jenkins-x/jx#8670 --- pkg/cmd/importcmd/pack.go | 17 +-- pkg/cmd/root/enable/enable.go | 10 -- pkg/cmd/root/root.go | 3 + pkg/cmd/root/version/version.go | 4 +- pkg/draft/draft.go | 40 +----- pkg/matcher/matcher.go | 46 ------- pkg/maven/archetype.go | 232 -------------------------------- pkg/quickstarts/model.go | 71 ---------- pkg/quickstarts/model_test.go | 12 -- 9 files changed, 15 insertions(+), 420 deletions(-) delete mode 100644 pkg/matcher/matcher.go delete mode 100644 pkg/maven/archetype.go diff --git a/pkg/cmd/importcmd/pack.go b/pkg/cmd/importcmd/pack.go index 372bf33a..e3d0a5d2 100644 --- a/pkg/cmd/importcmd/pack.go +++ b/pkg/cmd/importcmd/pack.go @@ -3,6 +3,7 @@ package importcmd import ( "fmt" "io" + "io/fs" "os" "path/filepath" "strings" @@ -11,7 +12,6 @@ import ( "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/chartutil" - "github.com/Azure/draft/pkg/osutil" "github.com/jenkins-x/jx-helpers/v3/pkg/files" "github.com/pkg/errors" ) @@ -73,14 +73,15 @@ func (p *Pack) SaveDir(dest, packName string) error { // save the rest of the files for relPath, f := range p.Files { path := filepath.Join(dest, relPath) - exists, err := osutil.Exists(path) + _, err := os.Stat(path) if err != nil { - return errors.Wrapf(err, "failed to check if path exists %s", path) - } - if !exists { - err := saveFile(path, f) - if err != nil { - return err + if errors.Is(err, fs.ErrNotExist) { + err := saveFile(path, f) + if err != nil { + return err + } + } else { + return fmt.Errorf("failed to check if path exists %s: %w", path, err) } } } diff --git a/pkg/cmd/root/enable/enable.go b/pkg/cmd/root/enable/enable.go index 5162d892..8441bfa6 100644 --- a/pkg/cmd/root/enable/enable.go +++ b/pkg/cmd/root/enable/enable.go @@ -10,9 +10,7 @@ import ( "github.com/jenkins-x-plugins/jx-project/pkg/cmd/importcmd" "github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper" "github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates" - "github.com/jenkins-x/lighthouse-client/pkg/triggerconfig" "github.com/spf13/cobra" - tektonv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" ) // Options contains the command line options @@ -32,14 +30,6 @@ var ( `) ) -// Trigger the found trigger configs -type Trigger struct { - Path string - Config *triggerconfig.Config - Names []string - Pipelines map[string]*tektonv1beta1.PipelineRun -} - // NewCmdPipelineEnable creates the command func NewCmdPipelineEnable() (*cobra.Command, *Options) { o := &Options{} diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 0ff04065..564a2130 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -4,6 +4,8 @@ import ( "fmt" "os" + "github.com/jenkins-x-plugins/jx-project/pkg/cmd/root/version" + "github.com/jenkins-x-plugins/jx-project/pkg/cmd/root/enable" "github.com/jenkins-x-plugins/jx-project/pkg/cmd/common" @@ -83,6 +85,7 @@ func NewCmdMain() (*cobra.Command, *WizardOptions) { cmd.AddCommand(NewCmdCreateSpring()) cmd.AddCommand(importcmd.NewCmdImport()) cmd.AddCommand(pullrequest.NewCmdCreatePullRequest()) + cmd.AddCommand(version.NewCmdVersion()) return cmd, options } diff --git a/pkg/cmd/root/version/version.go b/pkg/cmd/root/version/version.go index 90ecc71d..a70b8e9e 100644 --- a/pkg/cmd/root/version/version.go +++ b/pkg/cmd/root/version/version.go @@ -31,7 +31,7 @@ type Options struct { } // NewCmdVersion creates a command object for the "version" command -func NewCmdVersion() (*cobra.Command, *Options) { +func NewCmdVersion() *cobra.Command { o := &Options{} cmd := &cobra.Command{ @@ -42,7 +42,7 @@ func NewCmdVersion() (*cobra.Command, *Options) { helper.CheckErr(err) }, } - return cmd, o + return cmd } // Run implements the command diff --git a/pkg/draft/draft.go b/pkg/draft/draft.go index bd657741..6a88e5c3 100644 --- a/pkg/draft/draft.go +++ b/pkg/draft/draft.go @@ -4,54 +4,16 @@ import ( "fmt" "io" "os" - "path" "path/filepath" "strings" - "github.com/Azure/draft/pkg/draft/draftpath" - "github.com/Azure/draft/pkg/draft/pack/repo" - "github.com/Azure/draft/pkg/linguist" "github.com/jenkins-x/jx-logging/v3/pkg/log" ) -// copied from draft so we can change the $DRAFT_HOME to ~/.jx/draft and lookup jx draft packs +// copied from draft so we can change to lookup jx draft packs // credit original from: https://github.com/Azure/draft/blob/8e1a459/cmd/draft/create.go#L163 -// DoPackDetection performs pack detection across all the packs available in $(draft home)/packs in -// alphabetical order, returning the pack dirpath and any errors that occurred during the pack detection. -func DoPackDetection(home draftpath.Home, out io.Writer, dir string) (string, error) { - log.Logger().Infof("performing pack detection in folder %s", dir) - langs, err := linguist.ProcessDir(dir) - if err != nil { - return "", fmt.Errorf("there was an error detecting the language: %s", err) - } - if len(langs) == 0 { - return "", fmt.Errorf("there was an error detecting the language") - } - for _, lang := range langs { - detectedLang := linguist.Alias(lang) - fmt.Fprintf(out, "--> Draft detected %s (%f%%)\n", detectedLang.Language, detectedLang.Percent) - for _, repository := range repo.FindRepositories(home.Packs()) { - packDir := path.Join(repository.Dir, repo.PackDirName) - packs, err := os.ReadDir(packDir) - if err != nil { - return "", fmt.Errorf("there was an error reading %s: %v", packDir, err) - } - for _, file := range packs { - if file.IsDir() { - if strings.EqualFold(detectedLang.Language, file.Name()) { - packPath := filepath.Join(packDir, file.Name()) - return packPath, nil - } - } - } - } - fmt.Fprintf(out, "--> Could not find a pack for %s. Trying to find the next likely language match...\n", detectedLang.Language) - } - return "", fmt.Errorf("there was an error detecting the language using packs from %s", home.Packs()) -} - // DoPackDetectionForBuildPack performs detection of the language based on a sepcific build pack func DoPackDetectionForBuildPack(out io.Writer, dir, packDir string) (string, error) { log.Logger().Infof("performing pack detection in folder %s", dir) diff --git a/pkg/matcher/matcher.go b/pkg/matcher/matcher.go deleted file mode 100644 index 26924a26..00000000 --- a/pkg/matcher/matcher.go +++ /dev/null @@ -1,46 +0,0 @@ -package matcher - -import ( - "regexp" - - "github.com/pkg/errors" -) - -// Matcher matches strings using regex -type Matcher struct { - Includes []*regexp.Regexp - Excludes []*regexp.Regexp -} - -// Matches returns true if there are no includes or one of them matches and the text does not match an exclude -func (m *Matcher) Matches(text string) bool { - matches := len(m.Includes) == 0 - for _, re := range m.Includes { - if re.MatchString(text) { - matches = true - break - } - } - if !matches { - return false - } - for _, re := range m.Excludes { - if re.MatchString(text) { - return false - } - } - return true -} - -// ToRegexs creates a slice of regex -func (m *Matcher) ToRegexs(texts []string) ([]*regexp.Regexp, error) { - var answer []*regexp.Regexp - for _, text := range texts { - re, err := regexp.Compile(text) - if err != nil { - return answer, errors.Wrapf(err, "failed to parse regex: %s", text) - } - answer = append(answer, re) - } - return answer, nil -} diff --git a/pkg/maven/archetype.go b/pkg/maven/archetype.go deleted file mode 100644 index e47710cb..00000000 --- a/pkg/maven/archetype.go +++ /dev/null @@ -1,232 +0,0 @@ -package maven - -import ( - "fmt" - "sort" - "strings" - - "github.com/jenkins-x/jx-helpers/v3/pkg/input" - "github.com/jenkins-x/jx-helpers/v3/pkg/options" - "github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers" - "github.com/pkg/errors" -) - -const ( - MavenArchetypePluginVersion = "3.0.1" -) - -type ArtifactVersions struct { - GroupID string - ArtifactID string - Description string - Versions []string -} - -type GroupArchectypes struct { - GroupID string - Artifacts map[string]*ArtifactVersions -} - -type ArchetypeModel struct { - Groups map[string]*GroupArchectypes -} - -type ArtifactData struct { - GroupID string - ArtifactID string - Version string - Description string -} - -type ArchetypeFilter struct { - GroupIDs []string - GroupIDFilter string - ArtifactIDFilter string - Version string -} - -type ArchetypeForm struct { - ArchetypeGroupID string - ArchetypeArtifactID string - ArchetypeVersion string - - GroupID string - ArtifactID string - Package string - Version string -} - -func NewArchetypeModel() ArchetypeModel { - return ArchetypeModel{ - Groups: map[string]*GroupArchectypes{}, - } -} - -func (m *ArchetypeModel) GroupIDs(filter string) []string { - answer := []string{} - for group := range m.Groups { - if filter == "" || strings.Contains(group, filter) { - answer = append(answer, group) - } - } - sort.Strings(answer) - return answer -} - -func (m *ArchetypeModel) ArtifactIDs(groupID, filter string) []string { - answer := []string{} - artifact := m.Groups[groupID] - if artifact != nil { - for a := range artifact.Artifacts { - if filter == "" || strings.Contains(a, filter) { - answer = append(answer, a) - } - } - sort.Strings(answer) - } - return answer -} - -func (m *ArchetypeModel) Versions(groupID, artifactID, filter string) []string { - answer := []string{} - artifact := m.Groups[groupID] - if artifact != nil { - av := artifact.Artifacts[artifactID] - if av != nil { - for _, v := range av.Versions { - if filter == "" || strings.Contains(v, filter) { - answer = append(answer, v) - } - } - // TODO use a version sorter? - sort.Sort(sort.Reverse(sort.StringSlice(answer))) - } - } - return answer -} - -func (m *ArchetypeModel) AddArtifact(a *ArtifactData) *ArtifactVersions { - groupID := a.GroupID - artifactID := a.ArtifactID - version := a.Version - description := a.Description - if groupID == "" || artifactID == "" || version == "" { - return nil - } - - if m.Groups == nil { - m.Groups = map[string]*GroupArchectypes{} - } - group := m.Groups[groupID] - if group == nil { - group = &GroupArchectypes{ - GroupID: groupID, - Artifacts: map[string]*ArtifactVersions{}, - } - m.Groups[groupID] = group - } - artifact := group.Artifacts[artifactID] - if artifact == nil { - artifact = &ArtifactVersions{ - GroupID: groupID, - ArtifactID: artifactID, - Versions: []string{}, - } - group.Artifacts[artifactID] = artifact - } - if artifact.Description == "" && description != "" { - artifact.Description = description - } - if stringhelpers.StringArrayIndex(artifact.Versions, version) < 0 { - artifact.Versions = append(artifact.Versions, version) - } - return artifact -} - -func (m *ArchetypeModel) CreateSurvey(data *ArchetypeFilter, pickVersion bool, form *ArchetypeForm, i input.Interface) error { - groupIDs := data.GroupIDs - var err error - if len(data.GroupIDs) == 0 { - filteredGroups := m.GroupIDs(data.GroupIDFilter) - if len(filteredGroups) == 0 { - return options.InvalidOption("group-filter", data.GroupIDFilter, m.GroupIDs("")) - } - - // let's pick from all groups - form.ArchetypeGroupID, err = i.PickNameWithDefault(filteredGroups, "Group ID:", form.ArchetypeGroupID, "please pick the maven Group ID") - if err != nil { - return errors.Wrapf(err, "failed to pick Group ID") - } - artifactsWithoutFilter := m.ArtifactIDs(form.ArchetypeGroupID, "") - if len(artifactsWithoutFilter) == 0 { - return fmt.Errorf("could not find any artifacts for group %s", form.ArchetypeGroupID) - } - } else { - // TODO for now lets just support a single group ID being passed in - form.ArchetypeGroupID = groupIDs[0] - - artifactsWithoutFilter := m.ArtifactIDs(form.ArchetypeGroupID, "") - if len(artifactsWithoutFilter) == 0 { - return options.InvalidOption("group", form.ArchetypeGroupID, m.GroupIDs("")) - } - } - if form.ArchetypeGroupID == "" { - return fmt.Errorf("no archetype groupId selected") - } - - artifactIDs := m.ArtifactIDs(form.ArchetypeGroupID, data.ArtifactIDFilter) - if len(artifactIDs) == 0 { - artifactsWithoutFilter := m.ArtifactIDs(form.ArchetypeGroupID, "") - return options.InvalidOption("artifact", data.ArtifactIDFilter, artifactsWithoutFilter) - } - - if len(artifactIDs) == 1 { - form.ArchetypeArtifactID = artifactIDs[0] - } else { - form.ArchetypeArtifactID, err = i.PickNameWithDefault(artifactIDs, "Artifact ID:", form.ArchetypeArtifactID, "please pick the maven Artifact ID") - if err != nil { - return errors.Wrapf(err, "failed to pick Artifact ID") - } - } - if form.ArchetypeArtifactID == "" { - return fmt.Errorf("no archetype artifactId selected") - } - - version := data.Version - versions := m.Versions(form.ArchetypeGroupID, form.ArchetypeArtifactID, version) - if len(versions) == 0 { - return options.InvalidOption("version", version, m.Versions(form.ArchetypeGroupID, form.ArchetypeArtifactID, "")) - } - - if len(versions) == 1 || !pickVersion { - form.ArchetypeVersion = versions[0] - } else { - form.ArchetypeVersion, err = i.PickNameWithDefault(versions, "Version:", form.ArchetypeVersion, "please pick the maven version") - if err != nil { - return errors.Wrapf(err, "failed to pick version") - } - } - if form.ArchetypeVersion == "" { - return fmt.Errorf("no archetype version selected") - } - - if form.GroupID == "" { - form.GroupID, err = i.PickValue("Project Group ID:", "com.acme", true, "The maven Group ID used to default in the pom") - if err != nil { - return errors.Wrapf(err, "failed to pick Project Group ID") - } - } - if form.ArtifactID == "" { - form.ArtifactID, err = i.PickValue("Project Artifact ID:", "", true, "The maven Artifact ID used to default in the pom") - if err != nil { - return errors.Wrapf(err, "failed to pick Project Artifact ID") - } - } - if form.Version == "" { - form.Version, err = i.PickValue("Project Version:", "1.0.0-SNAPSHOT", true, "The maven Version used to default in the pom") - if err != nil { - return errors.Wrapf(err, "failed to pick Project Version") - } - } - return nil -} diff --git a/pkg/quickstarts/model.go b/pkg/quickstarts/model.go index f72f61d4..569d4aba 100644 --- a/pkg/quickstarts/model.go +++ b/pkg/quickstarts/model.go @@ -18,77 +18,6 @@ const ( JenkinsXQuickstartsOwner = "jenkins-x-quickstarts" ) -// GitQuickstart returns a github based quickstart -func GitQuickstart(owner, repo, version, downloadURL, language, framework string, tags ...string) *Quickstart { - return &Quickstart{ - ID: owner + "/" + repo, - Owner: owner, - Name: repo, - Version: version, - Language: language, - Framework: framework, - Tags: tags, - DownloadZipURL: downloadURL, - } -} - -// QuickStartVersion creates a quickstart version string -func QuickStartVersion(sha string) string { - return "1.0.0+" + sha -} - -/* -TODO - -func BranchArchiveURL(org string, name string, branch string) string { - return stringhelpers.UrlJoin("https://codeload.github.com", org, name, "zip", branch) -} - -// LoadGithubQuickstarts Loads quickstarts from github -func (model *QuickstartModel) LoadGithubQuickstarts(provider gits.GitProvider, owner string, includes []string, excludes []string) error { - repos, err := provider.ListRepositories(owner) - if err != nil { - return err - } - for _, repo := range repos { - name := repo.Name - if stringhelpers.StringMatchesAny(name, includes, excludes) { - model.Add(toGitHubQuickstart(provider, owner, repo)) - } - } - return nil -} - -func toGitHubQuickstart(provider gits.GitProvider, owner string, repo *gits.GitRepository) *Quickstart { - language := repo.Language - // TODO find this from GitHub??? - framework := "" - tags := []string{} - - branch := "master" - repoName := repo.Name - gitCommits, err := provider.ListCommits(owner, repoName, &gits.ListCommitsArguments{ - SHA: branch, - Page: 1, - PerPage: 1, - }) - version := "" - u := "" - if err != nil { - log.Logger().Warnf("failed to load commits on branch %s for repo %s/%s due to: %s", branch, owner, repoName, err.Error()) - } else if len(gitCommits) > 0 { - commit := gitCommits[0] - sha := commit.ShortSha() - version = QuickStartVersion(sha) - u = BranchArchiveURL(owner, repoName, sha) - } - if u == "" { - u = BranchArchiveURL(owner, repoName, "master") - } - return GitQuickstart(owner, repoName, version, u, language, framework, tags...) -} -*/ - // NewQuickstartModel creates a new quickstart model func NewQuickstartModel() *QuickstartModel { return &QuickstartModel{ diff --git a/pkg/quickstarts/model_test.go b/pkg/quickstarts/model_test.go index 7fea8007..1de8b76b 100644 --- a/pkg/quickstarts/model_test.go +++ b/pkg/quickstarts/model_test.go @@ -6,10 +6,8 @@ package quickstarts_test import ( "testing" - "github.com/blang/semver" "github.com/jenkins-x-plugins/jx-project/pkg/quickstarts" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestquickstartsQuickstartModelFilterText(t *testing.T) { @@ -228,13 +226,3 @@ func TestquickstartsQuickstartModelFilterDefaultsToNoMachineLearning(t *testing. assert.Contains(t, results, quickstart2) assert.NotContains(t, results, quickstart3) } - -func TestquickstartsQuickstartCreateVersion(t *testing.T) { - t.Parallel() - - sha := "d9e925718" - v := quickstarts.QuickStartVersion(sha) - sv, err := semver.Parse(v) - require.NoError(t, err, "failed to parse semantic version %s for quickstart", v) - t.Logf("parsed semantic version %s for quickstart", sv.String()) -}