Skip to content

Commit

Permalink
remove duplicated coded reported from codeclimate
Browse files Browse the repository at this point in the history
  • Loading branch information
mavimo committed Aug 23, 2018
1 parent 7c12a0b commit a6e5e5f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 57 deletions.
3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/thcyron/uiprogress"
"github.com/xetys/hetzner-kube/pkg/clustermanager"
"github.com/xetys/hetzner-kube/pkg/hetzner"
)

// DefaultConfigPath is the path where the default config is located
Expand Down Expand Up @@ -182,7 +183,7 @@ func (app *AppConfig) DeleteContextByName(name string) error {

// ActionProgress (deprecated)
func (app *AppConfig) ActionProgress(ctx context.Context, action *hcloud.Action) error {
errCh, progressCh := waitAction(ctx, app.Client, action)
errCh, progressCh := hetzner.WaitAction(ctx, app.Client, action)

if term.IsTerminal(os.Stdout) {
progress := uiprogress.New()
Expand Down
54 changes: 0 additions & 54 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,12 @@
package cmd

import (
"context"
"fmt"
"log"
"time"

"github.com/Pallinder/go-randomdata"
"github.com/hetznercloud/hcloud-go/hcloud"
)

func waitAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) (<-chan error, <-chan int) {
errCh := make(chan error, 1)
progressCh := make(chan int)

go func() {
defer close(errCh)
defer close(progressCh)

ticker := time.NewTicker(100 * time.Millisecond)

sendProgress := func(p int) {
select {
case progressCh <- p:
break
default:
break
}
}

for {
select {
case <-ctx.Done():
errCh <- ctx.Err()
return
case <-ticker.C:
}

action, _, err := client.Action.GetByID(ctx, action.ID)
if err != nil {
errCh <- ctx.Err()
return
}

switch action.Status {
case hcloud.ActionStatusRunning:
sendProgress(action.Progress)

case hcloud.ActionStatusSuccess:
sendProgress(100)
errCh <- nil
return
case hcloud.ActionStatusError:
errCh <- action.Error()
return
}
}
}()

return errCh, progressCh
}

func randomName() string {
return fmt.Sprintf("%s-%s%s", randomdata.Adjective(), randomdata.Noun(), randomdata.Adjective())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/hetzner/hetzner_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (provider *Provider) runCreateServer(opts *hcloud.ServerCreateOpts) (*hclou
}

func (provider *Provider) actionProgress(action *hcloud.Action) error {
errCh, progressCh := waitAction(provider.context, provider.client, action)
errCh, progressCh := WaitAction(provider.context, provider.client, action)

if term.IsTerminal(os.Stdout) {
progress := uiprogress.New()
Expand Down
3 changes: 2 additions & 1 deletion pkg/hetzner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func ProviderAndManager(context context.Context, cluster clustermanager.Cluster,
return provider, manager
}

func waitAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) (<-chan error, <-chan int) {
// WaitAction is an helper function used to wait for an action
func WaitAction(ctx context.Context, client *hcloud.Client, action *hcloud.Action) (<-chan error, <-chan int) {
errCh := make(chan error, 1)
progressCh := make(chan int)

Expand Down

0 comments on commit a6e5e5f

Please sign in to comment.