blob: 43578b2448e52103a3b5c8362f59c8cd11e8d438 [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 Lekveishvilid542b732024-03-25 18:17:39 +040014 repo string
15 sshKey string
16 port int
17 createAccountAddr string
18 loginAddr string
19 membershipsInitAddr string
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040020}
21
22func welcomeCmd() *cobra.Command {
23 cmd := &cobra.Command{
24 Use: "welcome",
25 RunE: welcomeCmdRun,
26 }
27 cmd.Flags().StringVar(
28 &welcomeFlags.repo,
29 "repo-addr",
30 "",
31 "",
32 )
33 cmd.Flags().StringVar(
34 &welcomeFlags.sshKey,
35 "ssh-key",
36 "",
37 "",
38 )
39 cmd.Flags().IntVar(
40 &welcomeFlags.port,
41 "port",
42 8080,
43 "",
44 )
Giorgi Lekveishvilic89b9002023-12-21 13:09:26 +040045 cmd.Flags().StringVar(
46 &welcomeFlags.createAccountAddr,
47 "create-account-addr",
48 "",
49 "",
50 )
Giorgi Lekveishvili83b72192024-03-11 18:36:14 +040051 cmd.Flags().StringVar(
52 &welcomeFlags.loginAddr,
53 "login-addr",
54 "",
55 "",
56 )
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +040057 cmd.Flags().StringVar(
58 &welcomeFlags.membershipsInitAddr,
59 "memberships-init-addr",
60 "",
61 "",
62 )
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040063 return cmd
64}
65
66func welcomeCmdRun(cmd *cobra.Command, args []string) error {
67 sshKey, err := os.ReadFile(welcomeFlags.sshKey)
68 if err != nil {
69 return err
70 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040071 signer, err := ssh.ParsePrivateKey(sshKey)
72 if err != nil {
73 return err
74 }
75 addr, err := soft.ParseRepositoryAddress(welcomeFlags.repo)
76 if err != nil {
77 return err
78 }
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040079 repo, err := soft.CloneRepository(addr, signer)
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040080 if err != nil {
81 return err
82 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040083 nsCreator, err := newNSCreator()
84 if err != nil {
85 return err
86 }
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040087 s := welcome.NewServer(
88 welcomeFlags.port,
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040089 installer.NewRepoIO(repo, signer),
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040090 nsCreator,
Giorgi Lekveishvilic89b9002023-12-21 13:09:26 +040091 welcomeFlags.createAccountAddr,
Giorgi Lekveishvili83b72192024-03-11 18:36:14 +040092 welcomeFlags.loginAddr,
Giorgi Lekveishvilid542b732024-03-25 18:17:39 +040093 welcomeFlags.membershipsInitAddr,
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040094 )
95 s.Start()
96 return nil
97}