This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (65 loc) · 2.11 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
78
79
80
81
82
83
package main
import (
"flag"
"os"
"strings"
latest "github.com/tcnksm/go-latest"
"github.com/FrangipaneTeam/terraform-templates/pkg/file"
"github.com/FrangipaneTeam/tf-doc-extractor/internal/example"
"github.com/FrangipaneTeam/tf-doc-extractor/internal/logger"
_ "embed"
)
//go:embed version.txt
var version string
func main() {
fileName := flag.String("filename", "", "filename")
exampleDir := flag.String("example-dir", "", "example directory")
fromTest := flag.Bool("test", false, "from test")
fromResource := flag.Bool("resource", false, "from resource")
debug := flag.Bool("debug", false, "sets log level to debug")
printVersion := flag.Bool("version", false, "prints version and exit")
flag.Parse()
version = strings.Trim(version, "\n")
logger.Logger = logger.SetupZeroLog(version, *debug)
logger.Logger.Info().Msgf("tf-doc-extractor version %s", version)
if *printVersion {
// check version
githubTag := &latest.GithubTag{
Owner: "FrangipaneTeam",
Repository: "tf-doc-extractor",
}
res, err := latest.Check(githubTag, version)
if err == nil {
if res.Outdated {
logger.Logger.Warn().Msgf("new version available : %s", res.Current)
}
} else {
logger.Logger.Warn().Err(err).Msg("failed to check version")
}
os.Exit(0)
}
if *fileName == "" {
logger.Logger.Fatal().Msg("filename is required")
}
if !file.IsFileExists(*fileName) {
logger.Logger.Fatal().Msgf("file %s not found", *fileName)
}
if !*fromTest && !*fromResource {
logger.Logger.Fatal().Msg("test or resource is required")
}
if *fromTest && *fromResource {
logger.Logger.Fatal().Msg("test and resource are exclusive")
}
logger.Logger.Info().Msgf("using file %s", *fileName)
if *fromTest {
errCreateExample := example.CreateExampleFile(*fileName, *exampleDir)
if errCreateExample != nil {
logger.Logger.Fatal().Err(errCreateExample).Msg("failed to create example file")
}
} else if *fromResource {
errCreateImport := example.CreateImportExampleFile(*fileName, *exampleDir)
if errCreateImport != nil {
logger.Logger.Fatal().Err(errCreateImport).Msg("failed to create example file")
}
}
}