-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
92 lines (75 loc) · 2.16 KB
/
docs.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
package main
import (
"embed"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"text/template"
"github.com/spf13/cobra"
)
const sampleProjectDir = "sample-project"
var (
//go:embed example-spec.yaml
exampleSpec string
//go:embed example-role.tf
exampleRole string
//go:embed sample-project
sampleProject embed.FS
)
var exampleSpecCmd = &cobra.Command{
Use: "example-spec",
Short: "Prints an example spec with extensive comments to stdout",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Print(exampleSpec)
return nil
},
}
var exampleRoleCmd = &cobra.Command{
Use: "example-role",
Short: "Prints an example IAM role in terraform format to stdout",
RunE: func(cmd *cobra.Command, args []string) error {
inlinePol, err := serializeRolePolicy(nil)
if err != nil {
return err
}
tpl := template.Must(template.New("example-role").Parse(exampleRole))
if err := tpl.Execute(os.Stdout, map[string]string{
"AssumeRolePolicy": defaultAssumeRolePolicy,
"InlinePolicy": inlinePol,
}); err != nil {
return err
}
return nil
},
}
var createSampleProjectCmd = &cobra.Command{
Use: "create-sample-project output-dir",
Short: "Creates a sample project in the given directory",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
outDir := args[0]
// Create the output directory if it doesn't exist
if err := os.MkdirAll(outDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory %s: %w", outDir, err)
}
// Copy the files over
d, _ := sampleProject.ReadDir(sampleProjectDir)
for _, f := range d {
p := filepath.Join(sampleProjectDir, f.Name())
b, _ := sampleProject.ReadFile(p)
outPath := filepath.Join(outDir, f.Name())
if err := ioutil.WriteFile(outPath, b, 0644); err != nil {
return fmt.Errorf("failed to write file %s: %w", outPath, err)
}
}
// Make run.sh executable
if err := os.Chmod(filepath.Join(outDir, "run.sh"), 0755); err != nil {
return fmt.Errorf("failed to make run.sh executable: %w", err)
}
log.Printf("Created sample project in '%s'.", outDir)
log.Printf("See '%s/run.sh' to get started.", outDir)
return nil
},
}