blob: 41d27f779cdd72b1b9e2a221c416df7e7684964b [file] [log] [blame]
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04001package main
2
3import (
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04004 "golang.org/x/crypto/ssh"
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04005 "os"
6
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +04007 "github.com/spf13/cobra"
8
9 "github.com/giolekva/pcloud/core/installer"
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040010 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040011 "github.com/giolekva/pcloud/core/installer/welcome"
12)
13
14var welcomeFlags struct {
Giorgi Lekveishvilic89b9002023-12-21 13:09:26 +040015 repo string
16 sshKey string
17 port int
18 createAccountAddr 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 Lekveishvili12850ee2023-06-22 13:11:17 +040050 return cmd
51}
52
53func welcomeCmdRun(cmd *cobra.Command, args []string) error {
54 sshKey, err := os.ReadFile(welcomeFlags.sshKey)
55 if err != nil {
56 return err
57 }
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040058 signer, err := ssh.ParsePrivateKey(sshKey)
59 if err != nil {
60 return err
61 }
62 addr, err := soft.ParseRepositoryAddress(welcomeFlags.repo)
63 if err != nil {
64 return err
65 }
Giorgi Lekveishvili57dffb32023-08-07 15:45:43 +040066 repo, err := soft.CloneRepository(addr, signer)
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040067 if err != nil {
68 return err
69 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040070 nsCreator, err := newNSCreator()
71 if err != nil {
72 return err
73 }
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040074 s := welcome.NewServer(
75 welcomeFlags.port,
Giorgi Lekveishvili94cda9d2023-07-20 10:16:09 +040076 installer.NewRepoIO(repo, signer),
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040077 nsCreator,
Giorgi Lekveishvilic89b9002023-12-21 13:09:26 +040078 welcomeFlags.createAccountAddr,
Giorgi Lekveishvili12850ee2023-06-22 13:11:17 +040079 )
80 s.Start()
81 return nil
82}