2022-12-20 05:49:37 +00:00
// Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx>
2022-12-31 06:14:08 +00:00
// SPDX-License-Identifier: Apache-2.0
2022-12-20 05:49:37 +00:00
package cmd
import (
"git.rob.mx/nidito/chinampa/pkg/command"
"git.rob.mx/nidito/joao/pkg/config"
"github.com/sirupsen/logrus"
)
2023-01-10 07:02:10 +00:00
var Diff = & command . Command {
2022-12-20 05:49:37 +00:00
Path : [ ] string { "diff" } ,
Summary : "Shows differences between local and remote configs" ,
2023-01-17 22:25:18 +00:00
Description : ` Fetches remote and compares against local, ignoring comments but respecting order. The diff output shows what would happen upon running ﹅joao fetch﹅. Specify ﹅--remote﹅ to show what would happen upon ﹅joao flush﹅ ` ,
2022-12-20 05:49:37 +00:00
Arguments : command . Arguments {
{
Name : "config" ,
Description : "The configuration file(s) to diff" ,
Required : false ,
Variadic : true ,
Values : & command . ValueSource {
2023-01-14 23:29:02 +00:00
Files : & fileExtensions ,
2022-12-20 05:49:37 +00:00
} ,
} ,
} ,
Options : command . Options {
"output" : {
Description : "How to format the differences" ,
2023-01-17 22:25:18 +00:00
Type : command . ValueTypeString ,
2022-12-20 05:49:37 +00:00
Default : "auto" ,
Values : & command . ValueSource {
Static : & [ ] string {
"auto" , "patch" , "exit-code" , "short" ,
} ,
} ,
} ,
2023-01-17 22:25:18 +00:00
"remote" : {
Description : "Shows what would happen on `flush` instead of `fetch`" ,
2023-01-27 20:30:45 +00:00
Type : command . ValueTypeBoolean ,
2023-01-17 22:25:18 +00:00
Default : false ,
} ,
2023-01-15 21:16:37 +00:00
"redacted" : {
Description : "Compare redacted versions" ,
2023-01-17 22:25:18 +00:00
Type : command . ValueTypeBoolean ,
2023-01-15 21:16:37 +00:00
Default : false ,
} ,
2022-12-20 05:49:37 +00:00
} ,
Action : func ( cmd * command . Command ) error {
paths := cmd . Arguments [ 0 ] . ToValue ( ) . ( [ ] string )
2023-01-15 21:16:37 +00:00
redacted := cmd . Options [ "redacted" ] . ToValue ( ) . ( bool )
2023-01-17 22:25:18 +00:00
remote := cmd . Options [ "remote" ] . ToValue ( ) . ( bool )
2022-12-20 05:49:37 +00:00
for _ , path := range paths {
local , err := config . Load ( path , false )
if err != nil {
return err
}
2023-01-17 22:25:18 +00:00
if err := local . DiffRemote ( path , redacted , remote , cmd . Cobra . OutOrStdout ( ) , cmd . Cobra . OutOrStderr ( ) ) ; err != nil {
2022-12-20 05:49:37 +00:00
return err
}
}
logrus . Info ( "Done" )
return nil
} ,
2023-01-10 07:02:10 +00:00
}