From f1f7a6f134ee5cd165c2f5abeb6d69c8ee6f60e6 Mon Sep 17 00:00:00 2001 From: danielbonilha Date: Thu, 3 Nov 2022 14:32:25 -0300 Subject: [PATCH] added agent.toml config (#47) * added agent.toml config * fixed connection with and without login flows * fixed connection with and without login flows --- agent/main.go | 91 +++++++++++++++++++++++++++++++++++++++---- client/cmd/connect.go | 16 +++++++- 2 files changed, 99 insertions(+), 8 deletions(-) diff --git a/agent/main.go b/agent/main.go index b580afa8..8245d4a1 100644 --- a/agent/main.go +++ b/agent/main.go @@ -2,21 +2,38 @@ package agent import ( "fmt" + "github.com/BurntSushi/toml" + "github.com/google/uuid" + "github.com/runopsio/hoop/common/grpc" pb "github.com/runopsio/hoop/common/proto" + "github.com/runopsio/hoop/common/version" "log" "os" - - "github.com/runopsio/hoop/common/grpc" - "github.com/runopsio/hoop/common/version" ) func Run() { fmt.Println(string(version.JSON())) - svrAddr := os.Getenv("SERVER_ADDRESS") - token := os.Getenv("TOKEN") + defaultServerAddress := "127.0.0.1:8010" + + conf := loadConfig() + if conf.Token == "" { + conf.Token = os.Getenv("TOKEN") + if conf.Token == "" { + conf.Token = "x-agt-" + uuid.NewString() + } + } + + if conf.ServerAddress == "" { + conf.ServerAddress = os.Getenv("SERVER_ADDRESS") + if conf.ServerAddress == "" { + conf.ServerAddress = defaultServerAddress + } + } + + saveConfig(conf) - client, err := grpc.Connect(svrAddr, token, grpc.WithOption("origin", pb.ConnectionOriginAgent)) + client, err := grpc.Connect(conf.ServerAddress, conf.Token, grpc.WithOption("origin", pb.ConnectionOriginAgent)) if err != nil { log.Fatal(err) } @@ -26,10 +43,70 @@ func Run() { agt := New(client, done) defer agt.Close() - go agt.Run(svrAddr, token) + go agt.Run(conf.ServerAddress, conf.Token) <-ctx.Done() if err := ctx.Err(); err != nil { log.Printf("error: %s", err.Error()) } log.Println("Server terminated connection... exiting...") } + +type ( + Config struct { + Token string + ServerAddress string + } +) + +func loadConfig() *Config { + path := getFilepath() + var conf Config + if _, err := toml.DecodeFile(path, &conf); err != nil { + panic(err) + } + + return &conf +} + +func saveConfig(conf *Config) { + f, err := os.OpenFile(getFilepath(), os.O_WRONLY, os.ModeAppend) + if err != nil { + panic(err) + } + defer f.Close() + + if err := f.Truncate(0); err != nil { + panic(err) + } + + f.Seek(0, 0) + + if err := toml.NewEncoder(f).Encode(conf); err != nil { + panic(err) + } +} + +func getFilepath() string { + home, err := os.UserHomeDir() + 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 +} diff --git a/client/cmd/connect.go b/client/cmd/connect.go index 7478b480..8d77f3f0 100644 --- a/client/cmd/connect.go +++ b/client/cmd/connect.go @@ -6,6 +6,7 @@ import ( "log" "os" "runtime" + "strings" "time" "github.com/briandowns/spinner" @@ -50,9 +51,22 @@ type connect struct { } func runConnect(args []string) { + defaultHost := "127.0.0.1" + defaultPort := "8010" + config := loadConfig() - if config.Token == "" { + if config.Host == "" { + config.Host = defaultHost + } + + if config.Port == "" { + config.Port = defaultPort + } + + if config.Host != "" && + !strings.HasPrefix(config.Host, defaultHost) && + config.Token == "" { if err := doLogin(nil); err != nil { panic(err) }