Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor fixes on display login, types and static checks #156

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ Hossted activate connects you're instance to the hossted platform and sends inst
hossted activate
`,
Run: func(cmd *cobra.Command, args []string) {

validTypes := map[string]bool{
"k8s": true,
"compose": true,
"standby": true,
}

if !validTypes[activate_type] {
fmt.Printf("\033[31mInvalid type: %s. Valid types are: k8s, compose\033[0m\n", activate_type)
os.Exit(1) // Exit the program with an error
}

// write activate_type to config file
config, _ := hossted.GetConfig() // Ignore error
// Assign back to config object
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
_, err = checkConfigFilePath()

if err != nil {
fmt.Println(err)
//fmt.Println(err)
}

// Set greetings
Expand Down Expand Up @@ -135,7 +135,7 @@ func checkConfigFilePath() (string, error) {
return "", err
}

fmt.Printf("\nNo existing config file. \nNew config file is created - %s \n\n", cfgPath)
//fmt.Printf("\nNo existing config file. \nNew config file is created - %s \n\n", cfgPath)

} else {
// Normal case
Expand Down
37 changes: 7 additions & 30 deletions hossted/service/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"

Expand All @@ -27,8 +25,8 @@ var (
MIMIR_USERNAME = "-"
HOSSTED_API_URL = "-"
HOSSTED_AUTH_TOKEN = "-"
HOSSTED_AUTH_URL = "-"
HOSSTED_CLIENT_ID = "-"
HOSSTED_AUTH_URL = "-"
///////////////////////////
HOSSTED_DEV_API_URL = "-"
MIMIR_DEV_URL = "-"
Expand Down Expand Up @@ -64,7 +62,7 @@ func HttpRequest(method, url, token string, body []byte) error {
}

if resp.StatusCode != 200 {
return fmt.Errorf("Error sending event, errcode: %d", resp.StatusCode)
return fmt.Errorf("rrror sending event, errcode: %d", resp.StatusCode)
}

respBody, err := io.ReadAll(resp.Body)
Expand All @@ -79,33 +77,12 @@ func HttpRequest(method, url, token string, body []byte) error {
}

if !apiResponse.Success {
return fmt.Errorf("API response indicates failure: %v\n", apiResponse)
return fmt.Errorf("api response indicates failure: %v", apiResponse)
}

return nil
}

func openBrowser(url string) error {
var cmd string
var args []string

switch runtime.GOOS {
case "windows":
cmd = "rundll32"
args = append(args, "url.dll,FileProtocolHandler", url)
case "darwin":
cmd = "open"
args = append(args, url)
case "linux":
cmd = "xdg-open"
args = append(args, url)
default:
return fmt.Errorf("unsupported platform")
}

return exec.Command(cmd, args...).Start()
}

type org struct {
Name string `json:"name"`
ID string `json:"id"`
Expand Down Expand Up @@ -157,17 +134,17 @@ func GetOrgs(tokenString string) ([]org, error) {
parts := strings.Split(tokenString, ".")
if len(parts) != 3 {

return nil, fmt.Errorf("Invalid token format")
return nil, fmt.Errorf("invalid token format")
}

decodedPayload, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, fmt.Errorf("Error decoding payload: %s", err)
return nil, fmt.Errorf("error decoding payload: %s", err)
}

var claims JWTClaims
if err := json.Unmarshal(decodedPayload, &claims); err != nil {
return nil, fmt.Errorf("Error unmarshalling payload: %s", err)
return nil, fmt.Errorf("error unmarshalling payload: %s", err)

}

Expand Down Expand Up @@ -232,7 +209,7 @@ func OrgUseCases(orgs []org) (orgID string, err error) {

}

if orgs != nil && len(orgs) == 1 {
if len(orgs) == 1 {
orgName, err := base64.StdEncoding.DecodeString(orgs[0].Name)
if err != nil {
return "", err
Expand Down
10 changes: 5 additions & 5 deletions hossted/service/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func Login(develMode bool) error {
return err
}

fmt.Printf("User Code: %s\n", loginResp.UserCode)
fmt.Printf("Verification URL Complete: %s\n", loginResp.VerificationURIComplete)
log.Printf("\033[32mVerification URL: %s\033[0m", loginResp.VerificationURIComplete)
log.Printf("\033[32mUser Code: %s\033[0m", loginResp.UserCode)
openBrowser(loginResp.VerificationURIComplete)
// Schedule pollAccessToken after loginResp.Interval seconds

Expand All @@ -53,9 +53,9 @@ func Login(develMode bool) error {
time.Sleep(interval)
err := pollAccessToken(develMode, loginResp)
if err != nil {
log.Println("Please visit the above verfication url to complete sign in")
log.Println("\033[33m Please visit the above verification URL to complete sign in and paste the user code\033[0m")
} else {
log.Println("Access token polled successfully.")
log.Println("\033[32m Access token polled successfully.\033[0m")
break // Exit the loop if polling is successful
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func acquireDeviceCode(develMode bool) (authloginresp authLoginResp, err error)
}

if resp.StatusCode != 200 {
return authLoginResp{}, fmt.Errorf("Registration Failed, Error %s", string(body))
return authLoginResp{}, fmt.Errorf("registration failed, error %s", string(body))
}

var loginresp authLoginResp
Expand Down
7 changes: 4 additions & 3 deletions hossted/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func WriteConfigWrapper(config Config) error {
os.MkdirAll(folder, os.ModePerm)
}

fmt.Printf("\nNo existing config file. \nNew config file is created - %s \n\n", cfgPath)
//fmt.Printf("\nNo existing config file. \nNew config file is created - %s \n\n", cfgPath)

return err
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func WriteConfig(w io.Writer, config Config) error {
writer := bufio.NewWriter(w)
err = writer.Flush()
if err != nil {
fmt.Println(err)
// fmt.Println(err)
}

return nil
Expand Down Expand Up @@ -487,7 +487,8 @@ func GetSoftwarePath() (string, error) {
// Try another path
path = "/opt/linnovate/run/software.txt"
if _, err := os.Stat(path); os.IsNotExist(err) {
return "", fmt.Errorf("Config file does not exists in both /opt/hossted/run/software.txt or /opt/linnovate/run/software.txt. Please check.\n%w\n", err)
//return "", fmt.Errorf("Config file does not exists in both /opt/hossted/run/software.txt or /opt/linnovate/run/software.txt. Please check.\n%w\n", err)
return "", err
} else {
return path, nil
}
Expand Down