login.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright © 2021 NAME HERE <EMAIL ADDRESS>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "os"
  17. "encoding/json"
  18. "io/ioutil"
  19. nodepb "github.com/gravitl/netmaker/grpc"
  20. "context"
  21. "github.com/spf13/cobra"
  22. )
  23. // loginCmd represents the login command
  24. var loginCmd = &cobra.Command{
  25. Use: "login",
  26. Short: "Get auth token",
  27. Long: `Get auth token`,
  28. SilenceUsage: true,
  29. RunE: func(cmd *cobra.Command, args []string) error {
  30. password, err := cmd.Flags().GetString("password")
  31. macaddress, err := cmd.Flags().GetString("macaddress")
  32. if err != nil {
  33. return err
  34. }
  35. home, err := os.UserHomeDir()
  36. data := SetConfiguration{
  37. MacAddress: macaddress,
  38. Password: password,
  39. }
  40. file, err := json.MarshalIndent(data, "", " ")
  41. err = ioutil.WriteFile(home + "/.wcconfig", file, 0644)
  42. if err != nil {
  43. return err
  44. }
  45. login := &nodepb.LoginRequest{
  46. Password: password,
  47. Macaddress: macaddress,
  48. }
  49. // RPC call
  50. res, err := client.Login(context.TODO(), login)
  51. if err != nil {
  52. return err
  53. }
  54. fmt.Printf("Token: %s\n", res.Accesstoken)
  55. tokenstring := []byte(res.Accesstoken)
  56. err = ioutil.WriteFile(home + "/.wctoken", tokenstring, 0644)
  57. if err != nil {
  58. return err
  59. }
  60. return err
  61. },
  62. }
  63. func init() {
  64. loginCmd.Flags().StringP("macaddress", "m", "", "The local macaddress")
  65. loginCmd.Flags().StringP("password", "p", "", "The password")
  66. loginCmd.MarkFlagRequired("password")
  67. loginCmd.MarkFlagRequired("macaddress")
  68. rootCmd.AddCommand(loginCmd)
  69. }
  70. type SetConfiguration struct {
  71. MacAddress string
  72. Password string
  73. }