This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
treewalk.go
163 lines (140 loc) · 3.93 KB
/
treewalk.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/apex/log"
"github.com/client9/codegen/shell"
yaml "gopkg.in/yaml.v2"
)
// TreeConfig is the project configuration.
type TreeConfig struct {
// these can be set by config
Source string `yaml:"source,omitempty"` // type of downloader to make
Exe string `yaml:"exe,omitempty"` // stuff for "raw"
Nametpl string `yaml:"nametpl,omitempty"` // stuff for "raw"
Config string `yaml:"config,omitempty"` // sets a custom location for goreleaser.yml config file
// these can not be set by config file
// and are set by the url/path
org string // github.com for now
owner string // ^ github username
name string // repo name
}
// LoadTreeConfig Loads config file.
func LoadTreeConfig(file string) (config TreeConfig, err error) {
f, err := os.Open(file)
if err != nil {
return
}
log.WithField("file", file).Debug("loading config file")
return LoadTreeConfigReader(f)
}
// LoadTreeConfigReader config via io.Reader.
func LoadTreeConfigReader(fd io.Reader) (config TreeConfig, err error) {
data, err := ioutil.ReadAll(fd)
if err != nil {
return config, err
}
err = yaml.UnmarshalStrict(data, &config)
log.WithField("config", config).Debug("loaded config file")
return config, err
}
// treewalk walks the directory looking for .yaml files and generates
// downloader scripts from them. These are published to
// https://install.goreleaser.com
//
// see the following for performance improvement ideas:
// https://github.com/goreleaser/godownloader/issues/64
//
// nolint: funlen
func treewalk(root string, treeout string, forceWrite bool) error { // nolint: gocognit
rooterr := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// weird case where filewalk failed
if err != nil {
return err
}
// ignore directories
if info.IsDir() {
return nil
}
suffix := filepath.Ext(path)
// ignore non-yaml stuff
if suffix != ".yaml" && suffix != ".yml" {
return nil
}
// Now: root/github.com/owner/repo.yaml
rel, err := filepath.Rel(root, path)
if err != nil {
panic("should never happen.. path is always in root")
}
// Now: github.com/owner/repo.yaml
rel = rel[0 : len(rel)-len(suffix)]
// Now: github.com/owner/repo
// better way of doing this?
parts := strings.Split(rel, string(os.PathSeparator))
if len(parts) != 3 { //nolint:gomnd
return fmt.Errorf("invalid path: %s", path)
}
org, owner, repo := parts[0], parts[1], parts[2]
// Now: [ github.com client misspell ]
// only github.com for now
if org != "github.com" {
return fmt.Errorf("only github.com supported, got %s", org)
}
type repoConf struct {
Ignore bool `yaml:"ignore"`
}
var conf repoConf
bts, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if err := yaml.Unmarshal(bts, &conf); err != nil {
return err
}
if conf.Ignore {
log.WithField("repo", rel).Warn("ignoring repo as instructed")
return nil
}
// nice and clean
// org == github.com
// owner == you
// repo == your project
c, err := LoadTreeConfig(path)
if err != nil {
return err
}
// hacking for now and just hardwiring
if c.Source == "" {
c.Source = "godownloader"
}
// overwrite what exists for security
c.org = org
c.owner = owner
c.name = repo
shellcode, err := processSource(c.Source, owner+"/"+repo, c.Config, "", c.Exe, c.Nametpl)
if err != nil {
return err
}
// now write back
outdir := filepath.Join(treeout, org, owner)
err = os.MkdirAll(outdir, 0700)
if err != nil {
return err
}
shellpath := filepath.Join(outdir, repo+".sh")
// only write out if forced to, OR if output is effectively different
// than what the file has.
if forceWrite || shell.ShouldWriteFile(shellpath, shellcode) {
if err = ioutil.WriteFile(shellpath, shellcode, 0644); err != nil { // nolint: gosec
return err
}
}
// we did it!
return nil
})
return rooterr
}