(ctx context.Context, apiURL string, outputFile string, machine string, token string)
| 17 | ) |
| 18 | |
| 19 | func (cli *cliLapi) register(ctx context.Context, apiURL string, outputFile string, machine string, token string) error { |
| 20 | var err error |
| 21 | |
| 22 | lapiUser := machine |
| 23 | cfg := cli.cfg() |
| 24 | |
| 25 | if lapiUser == "" { |
| 26 | lapiUser, err = idgen.GenerateMachineID("") |
| 27 | if err != nil { |
| 28 | return fmt.Errorf("unable to generate machine id: %w", err) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pstr, err := idgen.GeneratePassword(idgen.PasswordLength) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | |
| 37 | password := strfmt.Password(pstr) |
| 38 | |
| 39 | apiurl, err := prepareAPIURL(cfg.API.Client, apiURL) |
| 40 | if err != nil { |
| 41 | return fmt.Errorf("parsing api url: %w", err) |
| 42 | } |
| 43 | |
| 44 | _, err = apiclient.RegisterClient(ctx, &apiclient.Config{ |
| 45 | MachineID: lapiUser, |
| 46 | Password: password, |
| 47 | RegistrationToken: token, |
| 48 | URL: apiurl, |
| 49 | VersionPrefix: LAPIURLPrefix, |
| 50 | }, nil) |
| 51 | if err != nil { |
| 52 | return fmt.Errorf("api client register: %w", err) |
| 53 | } |
| 54 | |
| 55 | log.Info("Successfully registered to Local API (LAPI)") |
| 56 | |
| 57 | var dumpFile string |
| 58 | |
| 59 | if outputFile != "" { |
| 60 | dumpFile = outputFile |
| 61 | } else if cfg.API.Client.CredentialsFilePath != "" { |
| 62 | dumpFile = cfg.API.Client.CredentialsFilePath |
| 63 | } else { |
| 64 | dumpFile = "" |
| 65 | } |
| 66 | |
| 67 | apiCfg := cfg.API.Client.Credentials |
| 68 | apiCfg.Login = lapiUser |
| 69 | apiCfg.Password = password.String() |
| 70 | |
| 71 | if apiURL != "" { |
| 72 | apiCfg.URL = apiURL |
| 73 | } |
| 74 | |
| 75 | apiConfigDump, err := yaml.Marshal(apiCfg) |
| 76 | if err != nil { |
no test coverage detected