Files
mgmt/main.go
James Shubin d52c90ede4 cli, entry, lang: Add an entry package for embeddable CLI's
This adds a new entry package that allows embedded programs to exist
inside of mgmt. This took a lot of refactoring to get the API right, but
I think it's incredibly elegant now. There is a chance we tweak things a
bit, but it's a good first start. All-in-one programs are coming soon!
2024-03-05 01:03:54 -05:00

93 lines
2.4 KiB
Go

// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
_ "embed"
"fmt"
"os"
"github.com/purpleidea/mgmt/cli"
cliUtil "github.com/purpleidea/mgmt/cli/util"
"github.com/purpleidea/mgmt/entry"
_ "github.com/purpleidea/mgmt/gapi/empty" // import so the gapi registers
_ "github.com/purpleidea/mgmt/lang/gapi" // import so the gapi registers
_ "github.com/purpleidea/mgmt/yamlgraph" // import so the gapi registers
"go.etcd.io/etcd/server/v3/etcdmain"
)
// These constants are some global variables that are used throughout the code.
const (
tagline = "next generation config management"
debug = false // add additional log messages
verbose = false // add extra log message output
)
// set at compile time
var (
program string
version string
)
//go:embed COPYING
var copying string
func main() {
// embed etcd completely
if len(os.Args) >= 2 && os.Args[1] == "etcd" {
args := []string{}
for i, s := range os.Args {
if i == 0 { // pop off our argv[0] and let `etcd` be it
continue
}
args = append(args, s)
}
etcdmain.Main(args) // this will os.Exit
return // for safety
}
data := &cliUtil.Data{
Program: program,
Version: version,
Copying: copying,
Tagline: tagline,
Flags: cliUtil.Flags{
Debug: debug,
Verbose: verbose,
},
Args: os.Args,
}
// is there an alternate entry point for the cli?
if cli, err := entry.Lookup(); err == nil {
if err := cli.CLI(context.Background(), data); err != nil {
fmt.Println(err)
os.Exit(1)
//return // redundant
}
return
}
if err := cli.CLI(context.Background(), data); err != nil {
fmt.Println(err)
os.Exit(1)
return
}
}