-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathreadme.go
More file actions
70 lines (53 loc) · 1.45 KB
/
readme.go
File metadata and controls
70 lines (53 loc) · 1.45 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
package pike
import (
"errors"
"fmt"
"os"
"path"
"strings"
"github.com/rs/zerolog/log"
)
type replaceSectionError struct {
err error
}
type fileDoesNotExistError struct {
file string
err error
}
func (e fileDoesNotExistError) Error() string {
return fmt.Sprintf("file %s does not exist %v", e.file, e.err)
}
func (m *replaceSectionError) Error() string {
return fmt.Sprintf("failed to replace section %v", m.err)
}
// Readme Updates a README.md file.
func Readme(dirName string, output string, init bool, autoAppend bool) error {
file := path.Join(dirName, "README.md")
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
return &fileDoesNotExistError{file, err}
}
OutPolicy, err := MakePolicy(dirName, nil, init, false, "", "")
if err != nil {
log.Info().Msg("failed to make policy")
return &makePolicyError{err}
}
var markdown string
switch strings.ToLower(output) {
case terraform:
markdown = "\nThe Terraform resource required is:\n\n```golang\n" + OutPolicy.AsString(output) + "\n```\n"
case "json":
markdown = "\nThe Policy required is:\n\n```json\n" + OutPolicy.AsString(output) + "\n```\n"
default:
return &tfPolicyFormatError{}
}
err = ReplaceSection(file, markdown, autoAppend)
if err != nil {
return &replaceSectionError{err}
}
log.Info().Msg("readme updated")
return err
}
type tfPolicyFormatError struct{}
func (m *tfPolicyFormatError) Error() string {
return "output formats are Terraform and JSON"
}