@ -33,7 +33,7 @@ type cmd struct {
func ( c * cmd ) getAppendFileNameFlag ( ) * flag . FlagSet {
fs := flag . NewFlagSet ( "" , flag . ContinueOnError )
fs . Var ( & c . appendFileNameFlag , "append-filename" , "Append filename flag supports the following " +
"comma-separated arguments. 1. version, 2. dc. 3. node 4. status. It appends these values to the filename provided in the command")
"comma-separated arguments. 1. version, 2. dc. It appends these values to the filename provided in the command")
return fs
}
@ -71,50 +71,35 @@ func (c *cmd) Run(args []string) int {
appendFileNameFlags := strings . Split ( c . appendFileNameFlag . String ( ) , "," )
if len ( appendFileNameFlags ) != 0 && len ( c . appendFileNameFlag . String ( ) ) > 0 {
agentSelfResponse , err := client . Agent ( ) . Self ( )
if err != nil {
c . UI . Error ( fmt . Sprintf ( "Error connecting to Consul agent and fetching datacenter/version: %s" , err ) )
return 1
}
fileExt := filepath . Ext ( file )
fileNameWithoutExt := strings . TrimSuffix ( file , fileExt )
if slices . Contains ( appendFileNameFlags , "version" ) {
if config , ok := agentSelfResponse [ "Config" ] ; ok {
if version , ok := config [ "Version" ] ; ok {
fileNameWithoutExt = fileNameWithoutExt + "-" + version . ( string )
operatorHealthResponse , err := client . Operator ( ) . AutopilotServerHealth ( nil )
if err != nil {
c . UI . Error ( fmt . Sprintf ( "Error fetching version of Consul agent Leader: %s" , err ) )
return 1
}
var version string
for _ , server := range operatorHealthResponse . Servers {
if server . Leader {
version = server . Version
break
}
}
fileNameWithoutExt = fileNameWithoutExt + "-" + version
}
if slices . Contains ( appendFileNameFlags , "dc" ) {
if config , ok := agentSelfResponse [ "Config" ] ; ok {
if datacenter , ok := config [ "Datacenter" ] ; ok {
fileNameWithoutExt = fileNameWithoutExt + "-" + datacenter . ( string )
}
agentSelfResponse , err := client . Agent ( ) . Self ( )
if err != nil {
c . UI . Error ( fmt . Sprintf ( "Error connecting to Consul agent and fetching datacenter/version: %s" , err ) )
return 1
}
}
if slices . Contains ( appendFileNameFlags , "node" ) {
if config , ok := agentSelfResponse [ "Config" ] ; ok {
if nodeName , ok := config [ "NodeName" ] ; ok {
fileNameWithoutExt = fileNameWithoutExt + "-" + nodeName . ( string )
}
}
}
if slices . Contains ( appendFileNameFlags , "status" ) {
if status , ok := agentSelfResponse [ "Stats" ] ; ok {
if config , ok := status [ "consul" ] ; ok {
configMap := config . ( map [ string ] interface { } )
if leader , ok := configMap [ "leader" ] ; ok {
if leader == "true" {
fileNameWithoutExt = fileNameWithoutExt + "-" + "leader"
} else {
fileNameWithoutExt = fileNameWithoutExt + "-" + "follower"
}
}
if datacenter , ok := config [ "Datacenter" ] ; ok {
fileNameWithoutExt = fileNameWithoutExt + "-" + datacenter . ( string )
}
}
}