-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·88 lines (71 loc) · 2.27 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
84
85
86
87
88
/*
Package summon is a library that allows giving a convenient command line to packed assets.
The binary created from this library is meant to be shared in a team to allow distribution
of common assets or code templates.
It solves the maintenance problem of multiple copies of same
code snippets distributed in many repos (like general makefile recipes), leveraging go modules and version
management. It also allows configuring a standard set of tools that a dev team can readily
invoke by name.
# Basics
This library needs a command entrypoint in a data repository. See https://github.com/davidovich/summon-example-assets.
It can be bootstrapped in an empty directory by using:
cd [empty data repo dir]
go run github.com/davidovich/summon/scaffold@latest init [module name]
*/
package summon
import (
"embed"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/davidovich/summon/cmd"
"github.com/davidovich/summon/pkg/summon"
)
// Main entrypoint, typically called from a data repository. Calling Main() relinquishes
// control to Summon so it can manage the command line arguments and instantiation of assets
// located in the embed.fs data repository parameter.
// Config opts functions are optional.
func Main(args []string, fs embed.FS, opts ...option) int {
options := &MainOptions{}
for _, o := range opts {
o(options)
}
summon.Name = args[0]
s, err := summon.New(fs)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to create initial filesystem: %v\n", err)
return 1
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
for range c {
os.Exit(0)
}
}()
rootCmd, err := cmd.CreateRootCmd(s, os.Args, *options)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create command tree: %v\n", err)
return 1
}
err = rootCmd.Execute()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
return exitError.ExitCode()
}
return 1
}
return 0
}
type option func(o *MainOptions)
// WithoutRunCmd configures summon to attach invocables directly to the
// main program. The default is to have these attached to the `run` subcommand.
func WithoutRunCmd() option {
return func(o *MainOptions) {
o.WithoutRunSubcmd = true
}
}
// MainOptions hold compile-time configurations.
type MainOptions = summon.MainOptions