Skip to content

Commit

Permalink
add interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
selimseker committed Aug 29, 2024
1 parent 52daf73 commit 336bf7c
Showing 1 changed file with 99 additions and 15 deletions.
114 changes: 99 additions & 15 deletions start/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"bufio"
"encoding/hex"
"flag"
"fmt"
"net/http"
Expand Down Expand Up @@ -71,19 +73,84 @@ func checkDockerCompose() string {
}

func checkRequiredEnvVars(envvars map[string]string) {
requiredVars := []string{
"DKN_WALLET_SECRET_KEY",
"DKN_ADMIN_PUBLIC_KEY",
absent_env := false
if envvars["DKN_WALLET_SECRET_KEY"] == "" {
fmt.Println("DKN_WALLET_SECRET_KEY env-var is not set, getting it interactively")
skey, err := getDknSecretKey()
if err != nil {
fmt.Printf("Error during user input: %s\n", err)
os.Exit(1)
}
envvars["DKN_WALLET_SECRET_KEY"] = skey
absent_env = true
}
for _, v := range requiredVars {
_, exists := envvars[v]
if !exists {
fmt.Printf("ERROR: %s environment variable is not set.\n", v)

if envvars["DKN_ADMIN_PUBLIC_KEY"] == "" {
fmt.Println("DKN_ADMIN_PUBLIC_KEY env-var is not set, getting it interactively")
akey, err := getDknAdminPublicKey()
if err != nil {
fmt.Printf("Error during user input: %s\n", err)
os.Exit(1)
}
envvars["DKN_ADMIN_PUBLIC_KEY"] = akey
absent_env = true
}

if absent_env {
// dump the .env file for future usages
fmt.Printf("Dumping the given env-vars to .env\n\n")
godotenv.Write(envvars, "./.env")

}
}

func getDknSecretKey() (string, error) {
reader := bufio.NewReader(os.Stdin)
// get DKN_WALLET_SECRET_KEY
fmt.Print("Please enter your DKN Wallet Secret Key (32-bytes hex encoded): ")
skey, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("couldn't get DKN Wallet Secret Key")
}
skey = strings.Split(skey, "\n")[0]
skey = strings.TrimSpace(skey)
skey = strings.TrimPrefix(skey, "0x")
// decode the hex string into bytes
decoded_skey, err := hex.DecodeString(skey)
if err != nil {
return "", fmt.Errorf("DKN Wallet Secret Key should be 32-bytes hex encoded")
}
// ensure the decoded bytes are exactly 32 bytes
if len(decoded_skey) != 32 {
return "", fmt.Errorf("DKN Wallet Secret Key should be 32 bytes long")
}
return skey, nil
}

func getDknAdminPublicKey() (string, error) {
reader := bufio.NewReader(os.Stdin)

// get DKN_ADMIN_PUBLIC_KEY
fmt.Print("Please enter DKN Admin Public Key (32-bytes hex encoded, you can get it from .env.example): ")
admin_pkey, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("couldn't get DKN Admin Public Key")
}
admin_pkey = strings.Split(admin_pkey, "\n")[0]
admin_pkey = strings.TrimSpace(admin_pkey)
admin_pkey = strings.TrimPrefix(admin_pkey, "0x")
// decode the hex string into bytes
decoded_admin_pkey, err := hex.DecodeString(admin_pkey)
if err != nil {
return "", fmt.Errorf("DKN Admin Public Key should be 32-bytes hex encoded")
}
// ensure the decoded bytes are exactly 33 bytes
if len(decoded_admin_pkey) != 33 {
return "", fmt.Errorf("DKN Admin Public Key should be 33 bytes long")
}
return admin_pkey, nil
}

func mapToList(m map[string]string) []string {
var list []string
for key, value := range m {
Expand Down Expand Up @@ -178,20 +245,37 @@ func main() {
os.Exit(0)
}

fmt.Println("Setting up the environment...")
fmt.Printf("Setting up the environment...\n\n")

// Check Docker Compose
composeCommand := checkDockerCompose()

// Load .env file if exists
var envvars map[string]string
envvars, err := godotenv.Read("./.env")
if err != nil {
fmt.Println("Error during loading .env: ", err)
os.Exit(1)
// if .env is not exists or required vars are absent, get them from terminal
fmt.Println("Couldn't load .env file")
fmt.Println("Getting required env vars interactively")
skey, err := getDknSecretKey()
if err != nil {
fmt.Printf("Error during user input: %s\n", err)
os.Exit(1)
}

akey, err := getDknAdminPublicKey()
if err != nil {
fmt.Printf("Error during user input: %s\n", err)
os.Exit(1)
}

envvars["DKN_WALLET_SECRET_KEY"] = skey
envvars["DKN_ADMIN_PUBLIC_KEY"] = akey
// dump the .env file for future usages
fmt.Printf("Dumping the given env-vars to .env\n\n")
godotenv.Write(envvars, "./.env")

}

// Check required environment variables
checkRequiredEnvVars(envvars)

// use models given with -m flag
Expand Down Expand Up @@ -254,9 +338,9 @@ func main() {
} else if runtime.GOOS == "linux" {
envvars["OLLAMA_HOST"] = LOCAL_HOST
DKN_DOCKER_NETWORK_MODE = "host"
// } else if runtime.GOOS == "windows" {
// // TODO test for windows
// envvars["OLLAMA_HOST"] = LOCAL_HOST
// } else if runtime.GOOS == "windows" {
// // TODO test for windows
// envvars["OLLAMA_HOST"] = LOCAL_HOST
}
} else {
// although --docker-ollama was not passed, we checked and couldnt find Ollama
Expand Down

0 comments on commit 336bf7c

Please sign in to comment.