2017-11-12 18:58:19 +00:00
package main
import (
2022-06-30 20:24:19 +00:00
"errors"
2022-02-14 16:43:34 +00:00
"fmt"
2021-07-16 18:28:13 +00:00
"io"
2022-06-30 20:24:19 +00:00
"io/fs"
2018-05-01 18:14:05 +00:00
"math/rand"
2021-07-16 18:28:13 +00:00
"os"
2022-06-30 20:24:19 +00:00
"path"
"runtime"
2018-05-01 18:14:05 +00:00
"time"
2017-11-12 18:58:19 +00:00
2021-06-26 04:46:51 +00:00
eefleetctl "github.com/fleetdm/fleet/v4/ee/fleetctl"
2017-11-12 18:58:19 +00:00
"github.com/kolide/kit/version"
2021-03-13 00:42:38 +00:00
"github.com/urfave/cli/v2"
2017-11-12 18:58:19 +00:00
)
2020-11-18 01:12:37 +00:00
const (
2022-03-21 17:53:53 +00:00
defaultFileMode = 0 o600
2020-11-18 01:12:37 +00:00
)
2018-05-01 18:14:05 +00:00
func init ( ) {
rand . Seed ( time . Now ( ) . UnixNano ( ) )
2017-11-12 18:58:19 +00:00
}
func main ( ) {
2022-02-14 16:43:34 +00:00
app := createApp ( os . Stdin , os . Stdout , exitErrHandler )
app . Run ( os . Args )
}
2021-07-16 18:28:13 +00:00
2022-02-14 16:43:34 +00:00
// exitErrHandler implements cli.ExitErrHandlerFunc. If there is an error, prints it to stderr and exits with status 1.
func exitErrHandler ( c * cli . Context , err error ) {
if err == nil {
return
}
2022-06-30 20:24:19 +00:00
2022-02-14 16:43:34 +00:00
fmt . Fprintf ( c . App . ErrWriter , "Error: %+v\n" , err )
2022-06-30 20:24:19 +00:00
if errors . Is ( err , fs . ErrPermission ) {
switch runtime . GOOS {
case "darwin" , "linux" :
fmt . Fprintf ( c . App . ErrWriter , "\nThis error can usually be resolved by fixing the permissions on the %s directory, or re-running this command with sudo.\n" , path . Dir ( c . String ( "config" ) ) )
case "windows" :
fmt . Fprintf ( c . App . ErrWriter , "\nThis error can usually be resolved by fixing the permissions on the %s directory, or re-running this command with 'Run as administrator'.\n" , path . Dir ( c . String ( "config" ) ) )
}
}
2022-02-14 16:43:34 +00:00
cli . OsExiter ( 1 )
2021-07-16 18:28:13 +00:00
}
func createApp ( reader io . Reader , writer io . Writer , exitErrHandler cli . ExitErrHandlerFunc ) * cli . App {
2018-05-01 18:14:05 +00:00
app := cli . NewApp ( )
app . Name = "fleetctl"
2020-12-10 19:26:00 +00:00
app . Usage = "CLI for operating Fleet"
2018-05-01 18:14:05 +00:00
app . Version = version . Version ( ) . Version
2021-07-16 18:28:13 +00:00
app . ExitErrHandler = exitErrHandler
2018-05-01 18:14:05 +00:00
cli . VersionPrinter = func ( c * cli . Context ) {
version . PrintFull ( )
2017-11-12 18:58:19 +00:00
}
2021-07-16 18:28:13 +00:00
app . Reader = reader
app . Writer = writer
app . ErrWriter = writer
2017-11-12 18:58:19 +00:00
2021-03-13 00:42:38 +00:00
app . Commands = [ ] * cli . Command {
2018-05-07 23:50:20 +00:00
applyCommand ( ) ,
2018-05-08 02:07:00 +00:00
deleteCommand ( ) ,
2018-05-04 16:53:21 +00:00
setupCommand ( ) ,
loginCommand ( ) ,
logoutCommand ( ) ,
2018-05-17 22:54:34 +00:00
queryCommand ( ) ,
2020-06-23 15:11:47 +00:00
getCommand ( ) ,
2021-09-14 13:58:35 +00:00
{
2018-05-01 22:58:53 +00:00
Name : "config" ,
2020-11-18 01:12:37 +00:00
Usage : "Modify Fleet server connection settings" ,
2021-03-13 00:42:38 +00:00
Subcommands : [ ] * cli . Command {
2018-05-04 16:53:21 +00:00
configSetCommand ( ) ,
configGetCommand ( ) ,
2018-05-01 22:58:53 +00:00
} ,
2018-05-01 18:14:05 +00:00
} ,
2018-05-21 16:26:22 +00:00
convertCommand ( ) ,
2020-01-24 05:27:20 +00:00
goqueryCommand ( ) ,
2020-11-05 01:06:55 +00:00
userCommand ( ) ,
2020-11-18 01:12:37 +00:00
debugCommand ( ) ,
2020-11-18 21:16:18 +00:00
previewCommand ( ) ,
2021-03-26 17:46:51 +00:00
eefleetctl . UpdatesCommand ( ) ,
2021-07-21 17:03:10 +00:00
hostsCommand ( ) ,
2021-09-14 13:58:35 +00:00
vulnerabilityDataStreamCommand ( ) ,
2021-09-09 05:34:12 +00:00
packageCommand ( ) ,
2022-08-11 19:26:52 +00:00
{
// It's become common for folks to unintentionally install fleetctl when they actually
// need the Fleet server. This is hopefully a more helpful error message.
Name : "prepare" ,
Usage : "This is not the binary you're looking for. Please use the fleet server binary for prepare commands." ,
Action : func ( c * cli . Context ) error {
return errors . New ( "This is not the binary you're looking for. Please use the fleet server binary for prepare commands." )
} ,
} ,
2017-11-12 18:58:19 +00:00
}
2021-07-16 18:28:13 +00:00
return app
2017-11-12 18:58:19 +00:00
}