/
init_git.go
101 lines (88 loc) · 2.6 KB
/
init_git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Copyright © 2022 Rémi Calizzano <remi.calizzano@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/openlizz/lizz/internal/repo"
)
var initGitCmd = &cobra.Command{
Use: "git",
Short: "Initialize the fleet repository in any Git server",
Long: `The init command is used to initialize the cluster by creating the fleet repository in any Git server.`,
RunE: initGitCmdRun,
}
type initGitFlags struct {
destinationUrl string
username string
password string
}
var initGitArgs initGitFlags
func init() {
initGitCmd.Flags().StringVar(&initGitArgs.destinationUrl, "destination-url", "", "Git repository URL")
initGitCmd.Flags().StringVarP(&initGitArgs.username, "username", "u", "git", "basic authentication username")
initGitCmd.Flags().StringVarP(&initGitArgs.password, "password", "p", "", "basic authentication password")
initCmd.AddCommand(initGitCmd)
}
func initGitCmdRun(cmd *cobra.Command, args []string) error {
logger.V(0).Infof("Initialize the cluster repository...")
var caBundle []byte
if initArgs.caFile != "" {
var err error
caBundle, err = os.ReadFile(initArgs.caFile)
if err != nil {
return fmt.Errorf("unable to read TLS CA file: %w", err)
}
}
clusterRepo, err := repo.CloneClusterRepo(
&repo.CloneOptions{
URL: initArgs.originUrl,
Branch: initArgs.originBranch,
Username: initGitArgs.username,
Password: initGitArgs.password,
PrivateKeyFile: initArgs.privateKeyFile,
Timeout: rootArgs.timeout,
CaBundle: caBundle,
},
status,
)
if err != nil {
return err
}
head, err := clusterRepo.Git().Head()
if err != nil {
return err
}
originUrl, _, err := repo.UniversalURL(initArgs.originUrl)
if err != nil {
return err
}
err = clusterRepo.NewClusterConfig(originUrl, head, status)
if err != nil {
return err
}
clusterRepo.CommitPush(
initArgs.authorName,
initArgs.authorEmail,
"Initialize cluster repository",
initGitArgs.destinationUrl,
initArgs.destinationBranch,
rootArgs.timeout,
status,
)
if err != nil {
return err
}
return nil
}