Skip to content

Commit

Permalink
remove code duplicated from hetzner-go package and various clanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mavimo committed Aug 15, 2018
1 parent aae7497 commit a299de8
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 85 deletions.
31 changes: 0 additions & 31 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import (
"os/user"
"path/filepath"

"github.com/go-kit/kit/log/term"
"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 @@ -181,34 +178,6 @@ func (app *AppConfig) DeleteContextByName(name string) error {
return fmt.Errorf("context '%s' not found", name)
}

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

if term.IsTerminal(os.Stdout) {
progress := uiprogress.New()

progress.Start()
bar := progress.AddBar(100).AppendCompleted().PrependElapsed()
bar.Empty = ' '

for {
select {
case err := <-errCh:
if err == nil {
bar.Set(100)
}
progress.Stop()
return err
case p := <-progressCh:
bar.Set(p)
}
}
} else {
return <-errCh
}
}

func (app *AppConfig) assertActiveContext() error {
if app.CurrentContext == nil {
return errors.New("no context selected")
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 @@ -272,7 +272,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)
progressCh, errCh := provider.client.Action.WatchProgress(provider.context, action)

if term.IsTerminal(os.Stdout) {
progress := uiprogress.New()
Expand Down
53 changes: 0 additions & 53 deletions pkg/hetzner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hetzner

import (
"context"
"time"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/xetys/hetzner-kube/pkg/clustermanager"
Expand All @@ -17,55 +16,3 @@ func ProviderAndManager(context context.Context, cluster clustermanager.Cluster,

return provider, manager
}

// 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)

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
}

0 comments on commit a299de8

Please sign in to comment.