-
Notifications
You must be signed in to change notification settings - Fork 153
/
git.go
100 lines (79 loc) · 3.12 KB
/
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
/*
Copyright 2021 The Flux authors
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 git
import (
"context"
"errors"
"path/filepath"
gogit "github.com/go-git/go-git/v5"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
var (
ErrNoGitRepository = errors.New("no git repository")
ErrNoStagedFiles = errors.New("no staged files")
)
type Author struct {
Name string
Email string
}
type Commit struct {
Author
Hash string
Message string
}
// WegoRoot is the default root directory for the GitOps repo
const WegoRoot = ".weave-gitops"
// WegoAppDir is where applications information will live in the GitOps repo
const WegoAppDir = "apps"
// WegoClusterDir is where cluster information and manifests will live in the GitOps repo
const WegoClusterDir = "clusters"
// WegoClusterOSWorkloadDir is where OS workload manifests will live in the GitOps repo
const WegoClusterOSWorkloadDir = "system"
// WegoClusterUserWorkloadDir is where user workload manifests will live in the GitOps repo
const WegoClusterUserWorkloadDir = "user"
func getClusterPath(clusterName string) string {
return filepath.Join(WegoRoot, WegoClusterDir, clusterName)
}
func GetSystemPath(clusterName string) string {
return filepath.Join(getClusterPath(clusterName), WegoClusterOSWorkloadDir)
}
func GetUserPath(clusterName string) string {
return filepath.Join(getClusterPath(clusterName), WegoClusterUserWorkloadDir)
}
// GetSystemQualifiedPath returns the path of the relativePath joined with the cluster's system directory
func GetSystemQualifiedPath(clusterName string, relativePath string) string {
return filepath.Join(GetSystemPath(clusterName), relativePath)
}
// GetProfilesPath returns the path of the file containing the manifests of installed Profiles
// joined with the cluster's system directory
func GetProfilesPath(clusterName, profilesManifestPath string) string {
return filepath.Join(GetSystemPath(clusterName), profilesManifestPath)
}
// Git is an interface for basic Git operations on a single branch of a
// remote repository.
//counterfeiter:generate . Git
type Git interface {
Open(path string) (*gogit.Repository, error)
Init(path, url, branch string) (bool, error)
Clone(ctx context.Context, path, url, branch string) (bool, error)
Checkout(newBranch string) error
Read(path string) ([]byte, error)
Write(path string, content []byte) error
Remove(path string) error
Commit(message Commit, filters ...func(string) bool) (string, error)
Push(ctx context.Context) error
Status() (bool, error)
Head() (string, error)
GetRemoteUrl(dir string, remoteName string) (string, error)
ValidateAccess(ctx context.Context, url string, branch string) error
}