Repository navigation
Expand file tree
/
Copy pathfetch.go
More file actions
71 lines (59 loc) · 1.58 KB
/
Copy pathfetch.go
File metadata and controls
71 lines (59 loc) · 1.58 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
func newFetchCmd() *cobra.Command {
return &cobra.Command{
Use: "fetch <config.toml>",
Short: "Manually fetch and deploy the project",
Long: `Manually run the deployment for the project that owns the current directory.`,
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
configPath = args[0]
return runFetch()
},
}
}
func runFetch() error {
if err := loadConfig(true); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
root, err := findRoot()
if err != nil {
return err
}
var repoConfig *RepoConfig
for _, repo := range config.Projects {
if repo.Path == root {
repoConfig = repo
break
}
}
if repoConfig == nil {
return fmt.Errorf("Deploy configuration was not found for the current directory.")
}
token, err := getRepositoryToken(repoConfig.Repository)
if err != nil {
return fmt.Errorf("failed to get credentials: %w", err)
}
return deploy(token, *repoConfig)
}
// findRoot returns the git repository root for the current working directory,
// falling back to the working directory itself when it is not inside a repo.
func findRoot() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to determine current directory: %w", err)
}
out, err := exec.Command("git", "-C", cwd, "rev-parse", "--show-toplevel").Output()
if err != nil {
return filepath.Abs(cwd)
}
return strings.TrimSpace(string(out)), nil
}