Skip to content

Commit

Permalink
added agent.toml config (#47)
Browse files Browse the repository at this point in the history
* added agent.toml config

* fixed connection with and without login flows

* fixed connection with and without login flows
  • Loading branch information
danielbonilha authored Nov 3, 2022
1 parent 35ce697 commit f1f7a6f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 8 deletions.
91 changes: 84 additions & 7 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
16 changes: 15 additions & 1 deletion client/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"runtime"
"strings"
"time"

"github.com/briandowns/spinner"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit f1f7a6f

Please sign in to comment.