-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
77 lines (65 loc) · 1.7 KB
/
main.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
package main
import (
"context"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/open-policy-agent/conftest/downloader"
"github.com/policy-hub/policy-hub-cli/internal/commands"
"github.com/policy-hub/policy-hub-cli/pkg/helpers"
)
func init() {
checkCreateDir()
checkCreateRegistriesList()
checkCreateRegistriesMetadata()
}
func main() {
if err := commands.NewRootCommand().Execute(); err != nil {
os.Exit(1)
}
}
func checkCreateRegistriesMetadata() {
metadataPath := helpers.MetadataConfigPath()
_, err := os.Stat(metadataPath)
if os.IsNotExist(err) {
registriesListPath := filepath.ToSlash(
filepath.Join(helpers.ConfigPath(), helpers.PolicyRegistryFilename))
content, err := ioutil.ReadFile(registriesListPath)
if err != nil {
log.Panic("could not read registries list file: %w", err)
}
lines := strings.Split(string(content), "\n")
err = downloader.Download(context.Background(), metadataPath, lines)
if err != nil {
log.Panic("could not fetch metadata file: %w", err)
}
}
}
func checkCreateRegistriesList() {
configPath := helpers.ConfigPath()
registriesListPath := filepath.ToSlash(
filepath.Join(configPath, helpers.PolicyRegistryFilename))
_, err := os.Stat(registriesListPath)
if os.IsNotExist(err) {
file, err := os.Create(registriesListPath)
defer file.Close()
if err != nil {
log.Panic("could not create file: %w", err)
}
_, err = io.WriteString(file, helpers.RegistryListDefault)
if err != nil {
log.Panic("could not write to file: %w", err)
}
file.Sync()
}
}
func checkCreateDir() {
configPath := helpers.ConfigPath()
_, err := os.Stat(configPath)
if os.IsNotExist(err) {
os.MkdirAll(configPath, os.ModePerm)
}
}