This is an automated email from the ASF dual-hosted git repository.

tsato pushed a commit to branch release-1.9.x
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 3cc05f2468cabb8a0c007fc7ec80633c7fbd1805
Author: Tadayoshi Sato <[email protected]>
AuthorDate: Tue Jul 26 20:22:28 2022 +0900

    chore(cli): refactor run command
---
 pkg/cmd/run.go          |   5 +-
 pkg/cmd/util_sources.go | 174 +++++++++++++++++++++++-------------------------
 2 files changed, 85 insertions(+), 94 deletions(-)

diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 2e55e1a99..dde1ea3ba 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -888,9 +888,8 @@ func getMountPath(targetPath string, dirName string, path 
string) (string, error
        return filepath.Join(targetPath, localRelativePath), nil
 }
 
-// nolint:errcheck
 func (o *runCmdOptions) uploadPomFromJar(gav maven.Dependency, path string, 
platform *v1.IntegrationPlatform, ns string, options spectrum.Options, cmd 
*cobra.Command) maven.Dependency {
-       util.WithTempDir("camel-k", func(tmpDir string) error {
+       _ = util.WithTempDir("camel-k", func(tmpDir string) error {
                pomPath := filepath.Join(tmpDir, "pom.xml")
                jar, err := zip.OpenReader(path)
                if err != nil {
@@ -922,7 +921,7 @@ func (o *runCmdOptions) uploadPomFromJar(gav 
maven.Dependency, path string, plat
                        } else {
                                gav.Type = "pom"
                                // Swallow error as this is not a mandatory step
-                               o.uploadAsMavenArtifact(gav, pomPath, platform, 
ns, options, cmd)
+                               _ = o.uploadAsMavenArtifact(gav, pomPath, 
platform, ns, options, cmd)
                        }
                }
                return nil
diff --git a/pkg/cmd/util_sources.go b/pkg/cmd/util_sources.go
index 991bf9eab..1c6563886 100644
--- a/pkg/cmd/util_sources.go
+++ b/pkg/cmd/util_sources.go
@@ -35,7 +35,7 @@ import (
        "github.com/pkg/errors"
 )
 
-// Source ---.
+// Source represents the source file of an Integration.
 type Source struct {
        Origin   string
        Location string
@@ -60,7 +60,7 @@ func (s *Source) setContent(content []byte) error {
        return nil
 }
 
-// ResolveSources ---.
+// ResolveSources resolves sources from a variety of locations including local 
and remote.
 func ResolveSources(ctx context.Context, locations []string, compress bool, 
cmd *cobra.Command) ([]Source, error) {
        sources := make([]Source, 0, len(locations))
 
@@ -71,7 +71,7 @@ func ResolveSources(ctx context.Context, locations []string, 
compress bool, cmd
                }
 
                if ok {
-                       answer, err := ResolveLocalSource(location, compress)
+                       answer, err := resolveLocalSource(location, compress)
                        if err != nil {
                                return sources, err
                        }
@@ -85,101 +85,26 @@ func ResolveSources(ctx context.Context, locations 
[]string, compress bool, cmd
 
                        switch {
                        case u.Scheme == gistScheme || 
strings.HasPrefix(location, "https://gist.github.com/";):
-                               var tc *http.Client
-
-                               if token, ok := os.LookupEnv("GITHUB_TOKEN"); 
ok {
-                                       ts := 
oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
-                                       tc = oauth2.NewClient(ctx, ts)
-
-                                       fmt.Fprintln(cmd.OutOrStdout(), 
"GITHUB_TOKEN env var detected, using it for GitHub APIs authentication")
-                               }
-
-                               gc := github.NewClient(tc)
-                               gistID := ""
-
-                               if strings.HasPrefix(location, 
"https://gist.github.com/";) {
-                                       names := 
util.FindNamedMatches(`^https://gist.github.com/(([a-zA-Z0-9]*)/)?(?P<gistid>[a-zA-Z0-9]*)$`,
 location)
-                                       if value, ok := names["gistid"]; ok {
-                                               gistID = value
-                                       }
-                               } else {
-                                       gistID = u.Opaque
-                               }
-
-                               if gistID == "" {
-                                       return sources, fmt.Errorf("unable to 
determining gist id from %s", location)
-                               }
-
-                               gists, _, err := gc.Gists.Get(ctx, gistID)
+                               answer, err := resolveGistSource(ctx, location, 
compress, cmd, u)
                                if err != nil {
                                        return sources, err
                                }
-
-                               for _, v := range gists.Files {
-                                       if v.Filename == nil || v.Content == 
nil {
-                                               continue
-                                       }
-
-                                       answer := Source{
-                                               Name:     *v.Filename,
-                                               Compress: compress,
-                                               Origin:   location,
-                                       }
-                                       if v.RawURL != nil {
-                                               answer.Location = *v.RawURL
-                                       }
-                                       if err := 
answer.setContent([]byte(*v.Content)); err != nil {
-                                               return sources, err
-                                       }
-                                       sources = append(sources, answer)
-                               }
+                               sources = append(sources, answer...)
                        case u.Scheme == githubScheme:
-                               answer := Source{
-                                       Name:     path.Base(location),
-                                       Origin:   location,
-                                       Location: location,
-                                       Compress: compress,
-                               }
-
-                               content, err := loadContentGitHub(ctx, u)
-                               if err != nil {
-                                       return sources, err
-                               }
-                               if err := answer.setContent(content); err != 
nil {
-                                       return sources, err
-                               }
-                               sources = append(sources, answer)
-                       case u.Scheme == httpScheme:
-                               answer := Source{
-                                       Name:     path.Base(location),
-                                       Origin:   location,
-                                       Location: location,
-                                       Compress: compress,
-                               }
-
-                               content, err := loadContentHTTP(ctx, u)
+                               answer, err := resolveSource(location, 
compress, func() ([]byte, error) {
+                                       return loadContentGitHub(ctx, u)
+                               })
                                if err != nil {
                                        return sources, err
                                }
-                               if err := answer.setContent(content); err != 
nil {
-                                       return sources, err
-                               }
                                sources = append(sources, answer)
-                       case u.Scheme == httpsScheme:
-                               answer := Source{
-                                       Name:     path.Base(location),
-                                       Origin:   location,
-                                       Location: location,
-                                       Compress: compress,
-                               }
-
-                               content, err := loadContentHTTP(ctx, u)
+                       case u.Scheme == httpScheme || u.Scheme == httpsScheme:
+                               answer, err := resolveSource(location, 
compress, func() ([]byte, error) {
+                                       return loadContentHTTP(ctx, u)
+                               })
                                if err != nil {
                                        return sources, err
                                }
-                               if err := answer.setContent(content); err != 
nil {
-                                       return sources, err
-                               }
                                sources = append(sources, answer)
                        default:
                                return sources, fmt.Errorf("missing file or 
unsupported scheme in %s", location)
@@ -190,23 +115,90 @@ func ResolveSources(ctx context.Context, locations 
[]string, compress bool, cmd
        return sources, nil
 }
 
-// ResolveLocalSource --.
-func ResolveLocalSource(location string, compress bool) (Source, error) {
+// resolveGistSource resolves sources from a Gist.
+func resolveGistSource(ctx context.Context, location string, compress bool, 
cmd *cobra.Command, u *url.URL) ([]Source, error) {
+       var hc *http.Client
+
+       if token, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
+               ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: 
token})
+               hc = oauth2.NewClient(ctx, ts)
+
+               fmt.Fprintln(cmd.OutOrStdout(), "GITHUB_TOKEN env var detected, 
using it for GitHub APIs authentication")
+       }
+
+       gc := github.NewClient(hc)
+       gistID := ""
+
+       if strings.HasPrefix(location, "https://gist.github.com/";) {
+               names := 
util.FindNamedMatches(`^https://gist.github.com/(([a-zA-Z0-9]*)/)?(?P<gistid>[a-zA-Z0-9]*)$`,
 location)
+               if value, ok := names["gistid"]; ok {
+                       gistID = value
+               }
+       } else {
+               gistID = u.Opaque
+       }
+
+       if gistID == "" {
+               return []Source{}, fmt.Errorf("unable to determining gist id 
from %s", location)
+       }
+
+       gists, _, err := gc.Gists.Get(ctx, gistID)
+       if err != nil {
+               return []Source{}, err
+       }
+
+       sources := make([]Source, 0, len(gists.Files))
+       for _, v := range gists.Files {
+               if v.Filename == nil || v.Content == nil {
+                       continue
+               }
+
+               answer := Source{
+                       Name:     *v.Filename,
+                       Compress: compress,
+                       Origin:   location,
+               }
+               if v.RawURL != nil {
+                       answer.Location = *v.RawURL
+               }
+               if err := answer.setContent([]byte(*v.Content)); err != nil {
+                       return sources, err
+               }
+               sources = append(sources, answer)
+       }
+
+       return sources, nil
+}
+
+// resolveLocalSource resolves a source from the local file system.
+func resolveLocalSource(location string, compress bool) (Source, error) {
        if _, err := os.Stat(location); err != nil && os.IsNotExist(err) {
                return Source{}, errors.Wrapf(err, "file %s does not exist", 
location)
        } else if err != nil {
                return Source{}, errors.Wrapf(err, "error while accessing file 
%s", location)
        }
 
+       answer, err := resolveSource(location, compress, func() ([]byte, error) 
{
+               return util.ReadFile(location)
+       })
+       if err != nil {
+               return Source{}, err
+       }
+       answer.Local = true
+
+       return answer, nil
+}
+
+// resolveSource resolves a source using the content provider function.
+func resolveSource(location string, compress bool, loadContent func() ([]byte, 
error)) (Source, error) {
        answer := Source{
                Name:     path.Base(location),
                Origin:   location,
                Location: location,
                Compress: compress,
-               Local:    true,
        }
 
-       content, err := util.ReadFile(location)
+       content, err := loadContent()
        if err != nil {
                return Source{}, err
        }

Reply via email to