Skip to content

Commit

Permalink
Fixes config issues
Browse files Browse the repository at this point in the history
Signed-off-by: Saahil Bhavsar <[email protected]>
  • Loading branch information
SaahilNotSahil committed Jun 7, 2024
1 parent 01cf35e commit 9001a42
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 225 deletions.
2 changes: 1 addition & 1 deletion autoproxy.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"base_url": "http://www.gstatic.com/generate_204",
"login_base_url": "https://gateway.iitj.ac.in:1003/"
"login_base_url": "https://gateway.iitj.ac.in:1003/",
"username": "",
"password": "",
"token": "",
Expand Down
10 changes: 6 additions & 4 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"syscall"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"

"github.com/SaahilNotSahil/iitj-autoproxy/pkg/keystore"
)

func init() {
Expand All @@ -30,7 +29,10 @@ var configCmd = &cobra.Command{
cobra.CheckErr(err)
password := string(bytePassword)

keystore.Set("username", username)
keystore.Set("password", password)
viper.Set("username", username)
viper.Set("password", password)
viper.Set("token", "")

cobra.CheckErr(viper.WriteConfig())
},
}
4 changes: 3 additions & 1 deletion cli/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

func init() {
loginCmd.Flags().BoolVarP(&isDummy, "dummy", "d", false, "")
loginCmd.Flags().BoolVarP(
&isDummy, "dummy", "d", false, "Run a dummy version of the command",
)
rootCmd.AddCommand(loginCmd)
}

Expand Down
4 changes: 3 additions & 1 deletion cli/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

func init() {
logoutCmd.Flags().BoolVarP(&isDummy, "dummy", "d", false, "")
logoutCmd.Flags().BoolVarP(
&isDummy, "dummy", "d", false, "Run a dummy version of the command",
)
rootCmd.AddCommand(logoutCmd)
}

Expand Down
19 changes: 19 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var isDummy bool

func initConfig() {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

viper.AddConfigPath(home)

viper.SetConfigName(".autoproxyconfig")
viper.SetConfigType("json")

cobra.CheckErr(viper.ReadInConfig())
}

func init() {
cobra.OnInitialize(initConfig)
}

var rootCmd = &cobra.Command{
Use: "autoproxy",
Short: "Use IITJ internet hassle-free",
Expand Down
4 changes: 3 additions & 1 deletion cli/cmd/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

func init() {
scheduleCmd.Flags().BoolVarP(&isDummy, "dummy", "d", false, "")
scheduleCmd.Flags().BoolVarP(
&isDummy, "dummy", "d", false, "Run a dummy version of the command",
)
rootCmd.AddCommand(scheduleCmd)
}

Expand Down
85 changes: 17 additions & 68 deletions daemon/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ import (

"github.com/SaahilNotSahil/iitj-autoproxy/pkg"
"github.com/SaahilNotSahil/iitj-autoproxy/pkg/daemon"
"github.com/SaahilNotSahil/iitj-autoproxy/pkg/keystore"
)

func LoginCmd() {
err := viper.ReadInConfig()
if err != nil {
pkg.Logger.Println(err)
log.Println(err)
pkg.Logger.Println("Error reading config file")
log.Println("Error reading config file")

err = daemon.SendMessageToCLI(
"Error reading config file. Please make sure the file exists and is valid",
"Error reading config file",
)
if err != nil {
pkg.Logger.Println(err)
Expand All @@ -29,16 +28,16 @@ func LoginCmd() {
return
}

var username string
var password string
username := viper.GetString("username")
password := viper.GetString("password")
baseURL := viper.GetString("base_url")

username, err = keystore.Get("username")
if err != nil {
pkg.Logger.Println(err)
log.Println(err)
if username == "" || password == "" {
pkg.Logger.Println("Please configure the application using the config command")
log.Println("Please configure the application using the config command")

err = daemon.SendMessageToCLI(
err.Error(),
"Please configure the application using the config command",
)
if err != nil {
pkg.Logger.Println(err)
Expand All @@ -50,25 +49,7 @@ func LoginCmd() {
return
}

password, err = keystore.Get("password")
if err != nil {
pkg.Logger.Println(err)
log.Println(err)

err = daemon.SendMessageToCLI(
err.Error(),
)
if err != nil {
pkg.Logger.Println(err)
log.Println(err)

return
}

return
}

err = pkg.Login(viper.GetString("base_url"), username, password)
err = pkg.Login(baseURL, username, password)
if err != nil {
pkg.Logger.Println(err)
log.Println(err)
Expand All @@ -93,29 +74,11 @@ func LoginCmd() {
func LoginDummyCmd() {
err := viper.ReadInConfig()
if err != nil {
pkg.Logger.Println(err)
log.Println(err)

err = daemon.SendMessageToCLI(
"Error reading config file. Please make sure the file exists and is valid",
)
if err != nil {
pkg.Logger.Println(err)
log.Println(err)

return
}

return
}

_, err = keystore.Get("username")
if err != nil {
pkg.Logger.Println(err)
log.Println(err)
pkg.Logger.Println("Error reading config file")
log.Println("Error reading config file")

err = daemon.SendMessageToCLI(
err.Error(),
"Error reading config file",
)
if err != nil {
pkg.Logger.Println(err)
Expand All @@ -127,23 +90,9 @@ func LoginDummyCmd() {
return
}

_, err = keystore.Get("password")
if err != nil {
pkg.Logger.Println(err)
log.Println(err)

err = daemon.SendMessageToCLI(
err.Error(),
)
if err != nil {
pkg.Logger.Println(err)
log.Println(err)

return
}

return
}
_ = viper.GetString("username")
_ = viper.GetString("password")
_ = viper.GetString("base_url")

err = daemon.SendMessageToCLI("Dummy login successful")
if err != nil {
Expand Down
Loading

0 comments on commit 9001a42

Please sign in to comment.