@@ -44,51 +44,30 @@ func runInstall(cmd *cobra.Command, args []string) error { // NOSONAR
4444 return fmt .Errorf ("repository must be in format: owner/repo" )
4545 }
4646
47- repository := args [0 ]
48- log . Infof ( "repository: %v" , repository )
49- if ! strings . Contains ( repository , "/" ) {
50- return fmt . Errorf ( "repository must be in format: owner/repo" )
47+ owner , repo , err := parseRepository ( args [0 ])
48+ if err != nil {
49+ _ = cmd . Help ()
50+ log . Fatal ( err )
5151 }
5252
53- parts := strings .Split (repository , "/" )
54- owner := parts [0 ]
55- repo := parts [1 ]
56-
5753 tag := "latest"
5854 if len (args ) > 1 {
5955 tag = args [1 ]
6056 }
61- //fmt.Printf("Processing: %s/%s:%s\n", owner, repo, tag)
62- styles .PrintKV ("Repository:" , fmt .Sprintf ("%s/%s:%s" , owner , repo , tag ))
6357
64- log .Info ("GOOS " , "runtime.GOOS" , runtime .GOOS )
65- log . Info ( "GOARCH " , "runtime.GOARCH" , runtime . GOARCH )
58+ log .Info ("runtime " , "GOOS" , runtime .GOOS , "GOARCH" , runtime .GOARCH )
59+ styles . PrintKV ( "Repository: " , fmt . Sprintf ( "%s/%s:%s" , owner , repo , tag ) )
6660
67- // Cache
68- dsn := "fscache://?appname=install-release&maxsize=10485760"
69- httpClient := & http.Client {
70- Transport : httpcache .NewTransport (dsn , httpcache .WithSWRTimeout (10 * time .Second )),
71- }
72- // Client
73- client := github .NewClient (httpClient )
61+ client := getClient ()
7462
75- // Release
76- ctx := context .Background ()
77- var release * github.RepositoryRelease
78- if tag == "latest" {
79- release , _ , err = client .Repositories .GetLatestRelease (ctx , owner , repo )
80- } else {
81- release , _ , err = client .Repositories .GetReleaseByTag (ctx , owner , repo , tag )
82- }
83- //release, err := getRelease(client, owner, repo, tag)
63+ release , err := getRelease (client , owner , repo , tag )
8464 if err != nil {
8565 return fmt .Errorf ("get release error: %w" , err )
8666 }
8767 if verbose >= 3 {
8868 log .Debugf ("release: %v" , release )
8969 }
9070
91- //fmt.Printf("Installing Version: %s\n", release.GetTagName())
9271 styles .PrintKV ("Version:" , release .GetTagName ())
9372
9473 // Asset
@@ -127,12 +106,11 @@ func runInstall(cmd *cobra.Command, args []string) error { // NOSONAR
127106 }
128107
129108 log .Infof ("id: %v" , asset .GetID ())
130- //fmt.Printf("url: %s\n", asset.GetBrowserDownloadURL())
131109 log .Infof ("url: %v" , asset .GetBrowserDownloadURL ())
132110 styles .PrintKV ("Asset Name:" , asset .GetName ())
133111
134112 rc , _ , err := client .Repositories .DownloadReleaseAsset (
135- ctx , owner , repo , asset .GetID (), http .DefaultClient ,
113+ context . Background () , owner , repo , asset .GetID (), http .DefaultClient ,
136114 )
137115 if err != nil {
138116 return err
@@ -258,10 +236,8 @@ func runInstall(cmd *cobra.Command, args []string) error { // NOSONAR
258236 }
259237 }
260238
261- // WIP
262- pathmgr .CheckBinPath (binPath )
239+ pathmgr .CheckBinPath (binPath ) // WIP
263240
264- //fmt.Printf("\nSuccessfully Installed: %s\n", destName)
265241 styles .PrintKV ("Installed:" , destName )
266242 return nil
267243}
@@ -395,17 +371,25 @@ func findMatch(assets []*github.ReleaseAsset, os string, aliases []string) int {
395371 return - 1
396372}
397373
398- //func getRelease(client *github.Client, owner, repo, tag string) (*github.RepositoryRelease, error) {
399- // ctx := context.Background()
400- // var release *github.RepositoryRelease
401- // var err error
402- // if tag == "latest" {
403- // release, _, err = client.Repositories.GetLatestRelease(ctx, owner, repo)
404- // } else {
405- // release, _, err = client.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
406- // }
407- // if err != nil {
408- // return nil, fmt.Errorf("get release error: %w", err)
409- // }
410- // return release, nil
411- //}
374+ func getClient () * github.Client {
375+ dsn := "fscache://?appname=install-release&maxsize=10485760"
376+ httpClient := & http.Client {
377+ Transport : httpcache .NewTransport (dsn , httpcache .WithSWRTimeout (10 * time .Second )),
378+ }
379+ return github .NewClient (httpClient )
380+ }
381+
382+ func getRelease (client * github.Client , owner , repo , tag string ) (* github.RepositoryRelease , error ) {
383+ ctx := context .Background ()
384+ var release * github.RepositoryRelease
385+ var err error
386+ if tag == "" || tag == "latest" {
387+ release , _ , err = client .Repositories .GetLatestRelease (ctx , owner , repo )
388+ } else {
389+ release , _ , err = client .Repositories .GetReleaseByTag (ctx , owner , repo , tag )
390+ }
391+ if err != nil {
392+ return nil , fmt .Errorf ("get release error: %w" , err )
393+ }
394+ return release , nil
395+ }
0 commit comments