blob: b1f325901f120edb05164a4694fcbde534fe31d0 [file] [log] [blame]
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04001package main
2
3import (
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04004 "os"
5
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04006 "github.com/giolekva/pcloud/core/installer"
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +04007 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04008 "github.com/giolekva/pcloud/core/installer/welcome"
Giorgi Lekveishvili83b72192024-03-11 18:36:14 +04009 "github.com/spf13/cobra"
10 "golang.org/x/crypto/ssh"
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040011)
12
13var welcomeFlags struct {
Giorgi Lekveishvilic89b9002023-12-21 13:09:26 +040014 repo string
15 sshKey string
16 port int
17 createAccountAddr string
Giorgi Lekveishvili83b72192024-03-11 18:36:14 +040018 loginAddr string
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040019}
20
21func welcomeCmd() *cobra.Command {
22 cmd := &cobra.Command{
23 Use: "welcome",
24 RunE: welcomeCmdRun,
25 }
26 cmd.Flags().StringVar(
27 &welcomeFlags.repo,
28 "repo-addr",
29 "",
30 "",
31 )
32 cmd.Flags().StringVar(
33 &welcomeFlags.sshKey,
34 "ssh-key",
35 "",
36 "",
37 )
38 cmd.Flags().IntVar(
39 &welcomeFlags.port,
40 "port",
41 8080,
42 "",
43 )
Giorgi Lekveishvilic89b9002023-12-21 13:09:26 +040044 cmd.Flags().StringVar(
45 &welcomeFlags.createAccountAddr,
46 "create-account-addr",
47 "",
48 "",
49 )
Giorgi Lekveishvili83b72192024-03-11 18:36:14 +040050 cmd.Flags().StringVar(
51 &welcomeFlags.loginAddr,
52 "login-addr",
53 "",
54 "",
55 )
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040056 return cmd
57}
58
59func welcomeCmdRun(cmd *cobra.Command, args []string) error {
60 sshKey, err := os.ReadFile(welcomeFlags.sshKey)
61 if err != nil {
62 return err
63 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040064 signer, err := ssh.ParsePrivateKey(sshKey)
65 if err != nil {
66 return err
67 }
68 addr, err := soft.ParseRepositoryAddress(welcomeFlags.repo)
69 if err != nil {
70 return err
71 }
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040072 repo, err := soft.CloneRepository(addr, signer)
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040073 if err != nil {
74 return err
75 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040076 nsCreator, err := newNSCreator()
77 if err != nil {
78 return err
79 }
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040080 s := welcome.NewServer(
81 welcomeFlags.port,
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040082 installer.NewRepoIO(repo, signer),
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040083 nsCreator,
Giorgi Lekveishvilic89b9002023-12-21 13:09:26 +040084 welcomeFlags.createAccountAddr,
Giorgi Lekveishvili83b72192024-03-11 18:36:14 +040085 welcomeFlags.loginAddr,
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040086 )
87 s.Start()
88 return nil
89}