Skip to content

Commit

Permalink
Bugfix/hoop demo/start (#79)
Browse files Browse the repository at this point in the history
* Fix demo bug when user was logged in
  • Loading branch information
sandromello authored Dec 7, 2022
1 parent 7650ba0 commit 0d5bbc7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
20 changes: 2 additions & 18 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/BurntSushi/toml"
"github.com/google/uuid"
"github.com/runopsio/hoop/common/clientconfig"
"github.com/runopsio/hoop/common/grpc"
pb "github.com/runopsio/hoop/common/proto"
"github.com/runopsio/hoop/common/version"
Expand Down Expand Up @@ -121,26 +122,9 @@ func saveConfig(conf *Config) {
}

func getFilepath() string {
home, err := os.UserHomeDir()
filepath, err := clientconfig.NewPath(clientconfig.AgentFile)
if err != nil {
panic(err)
}

path := fmt.Sprintf("%s/.hoop", home)
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, 0700); err != nil {
panic(err)
}
}

filepath := fmt.Sprintf("%s/agent.toml", path)
if _, err := os.Stat(filepath); os.IsNotExist(err) {
f, err := os.Create(filepath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
}

return filepath
}
28 changes: 6 additions & 22 deletions client/cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package cmd

import (
"fmt"
"log"
"os"
"strings"

"github.com/BurntSushi/toml"
"github.com/briandowns/spinner"
"github.com/runopsio/hoop/common/clientconfig"
"github.com/runopsio/hoop/common/grpc"
"github.com/runopsio/hoop/common/memory"
pb "github.com/runopsio/hoop/common/proto"
"log"
"os"
"strings"
)

type (
Expand Down Expand Up @@ -50,27 +51,10 @@ func saveConfig(conf *Config) {
}

func getFilepath() string {
home, err := os.UserHomeDir()
filepath, err := clientconfig.NewPath(clientconfig.ClientFile)
if err != nil {
panic(err)
}

path := fmt.Sprintf("%s/.hoop", home)
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, 0700); err != nil {
panic(err)
}
}

filepath := fmt.Sprintf("%s/config.toml", path)
if _, err := os.Stat(filepath); os.IsNotExist(err) {
f, err := os.Create(filepath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
}

return filepath
}

Expand Down
2 changes: 1 addition & 1 deletion client/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func runExec(args []string) {
loader := spinner.New(spinner.CharSets[78], 70*time.Millisecond)
loader.Color("green")
loader.Start()
loader.Suffix = "executing input..."
loader.Suffix = " executing input ..."

c := newClientConnect(config, loader, args, pb.ClientVerbExec)
pkt := &pb.Packet{
Expand Down
17 changes: 17 additions & 0 deletions client/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/runopsio/hoop/client/cmd/demos"
"github.com/runopsio/hoop/client/cmd/styles"
"github.com/runopsio/hoop/common/clientconfig"
"github.com/runopsio/hoop/gateway"

"github.com/briandowns/spinner"
Expand Down Expand Up @@ -105,6 +106,9 @@ var startCmd = &cobra.Command{
fmt.Println(styles.Fainted.Render(" • Stop the demo"))
fmt.Println(styles.Default.Render(" $ docker stop hoopdemo"))
fmt.Println()
// best-effort to rename the config file when starting the demo.
// this fixes errors when trying the demo if the user has logged in before.
renameClientConfigs()
os.Exit(0)
}
index++
Expand All @@ -119,6 +123,19 @@ var startCmd = &cobra.Command{
},
}

func renameClientConfigs() {
if filepath, err := clientconfig.NewPath(clientconfig.AgentFile); err == nil {
if fi, _ := os.Stat(filepath); fi != nil && fi.Size() > 0 {
_ = os.Rename(filepath, fmt.Sprintf("%s-bkp", filepath))
}
}
if filepath, err := clientconfig.NewPath(clientconfig.ClientFile); err == nil {
if fi, _ := os.Stat(filepath); fi != nil && fi.Size() > 0 {
_ = os.Rename(filepath, fmt.Sprintf("%s-bkp", filepath))
}
}
}

var startAgentCmd = &cobra.Command{
Use: "agent",
Short: "Runs the agent component",
Expand Down
33 changes: 33 additions & 0 deletions common/clientconfig/clientconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package clientconfig

import (
"fmt"
"os"
)

const (
AgentFile string = "agent.toml"
ClientFile string = "config.toml"
)

func NewPath(configFile string) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed obtaing home dir, err=%v", err)
}
hoopHomeDir := fmt.Sprintf("%s/.hoop", home)
if _, err := os.Stat(hoopHomeDir); os.IsNotExist(err) {
if err := os.MkdirAll(hoopHomeDir, 0700); err != nil {
return "", fmt.Errorf("failed creating hoop home dir (%s), err=%v", hoopHomeDir, err)
}
}
filepath := fmt.Sprintf("%s/%s", hoopHomeDir, configFile)
if _, err := os.Stat(filepath); os.IsNotExist(err) {
f, err := os.Create(filepath)
if err != nil {
return filepath, fmt.Errorf("failed creating config file (%s), err=%v", filepath, err)
}
f.Close()
}
return filepath, nil
}

0 comments on commit 0d5bbc7

Please sign in to comment.