Skip to content

Commit 4536666

Browse files
authored
Output generated config_db.json to different file than the current one (#13)
1 parent dc4fd57 commit 4536666

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

cmd/generate.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var generateCmd = &cobra.Command{
1515
Use: "generate",
1616
Short: "Generate a config_db.json",
1717
Run: func(cmd *cobra.Command, args []string) {
18-
configDBFile, _ := cmd.Flags().GetString("output")
18+
configDBFile, _ := cmd.Flags().GetString("config-db")
1919
configDBBytes, err := os.ReadFile(configDBFile)
2020
if err != nil {
2121
fmt.Printf("failed to read current config file, %v\n", err)
@@ -44,7 +44,7 @@ var generateCmd = &cobra.Command{
4444
os.Exit(1)
4545
}
4646

47-
inputFile, _ := cmd.Flags().GetString("input")
47+
inputFile, _ := cmd.Flags().GetString("input-file")
4848
inputBytes, err := os.ReadFile(inputFile)
4949
if err != nil {
5050
fmt.Printf("failed to read input file, %v\n", err)
@@ -69,7 +69,8 @@ var generateCmd = &cobra.Command{
6969
os.Exit(1)
7070
}
7171

72-
err = os.WriteFile(configDBFile, configDBBytes, 0644) //nolint:gosec
72+
outputFile, _ := cmd.Flags().GetString("output-file")
73+
err = os.WriteFile(outputFile, configDBBytes, 0644) //nolint:gosec
7374
if err != nil {
7475
fmt.Printf("failed to write file, %v", err)
7576
os.Exit(1)
@@ -80,7 +81,8 @@ var generateCmd = &cobra.Command{
8081
func init() {
8182
rootCmd.AddCommand(generateCmd)
8283

83-
generateCmd.Flags().StringP("input", "i", "sonic-config.yaml", "path to input file to generate the config_db.json from")
84-
generateCmd.Flags().StringP("output", "o", "/etc/sonic/config_db.json", "path to output file")
84+
generateCmd.Flags().StringP("input-file", "i", "sonic-config.yaml", "path to input file to generate the config_db.json from")
85+
generateCmd.Flags().StringP("output-file", "o", "config_db.json", "path to output file")
8586
generateCmd.Flags().String("device-dir", "/usr/share/sonic/device", "directory which holds all device-specific files")
87+
generateCmd.Flags().StringP("config-db", "c", "/etc/sonic/config_db.json", "path to current config_db.json")
8688
}

tests/test.sh

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
#!/bin/bash
22

3-
function test() {
4-
test_dir=$1
5-
cp $test_dir/current-config_db.json $test_dir/config_db.json
3+
for i in 1 2 3
4+
do
5+
test_dir=$(pwd)/tests/$i
6+
docker run --rm -v $(pwd)/tests/device:/usr/share/sonic/device:ro -v $test_dir:/sonic sonic-configdb-utils:local generate -c /sonic/current-config_db.json -i /sonic/sonic-config.yaml -o /sonic/config_db.json
7+
diff --color=always $test_dir/expected.json $test_dir/config_db.json
68

7-
docker run --rm -v $(pwd)/tests/device:/usr/share/sonic/device:ro -v $test_dir:/etc/sonic -v $test_dir:/sonic sonic-configdb-utils:local generate
8-
9-
if [ $? -eq 1 ]; then
10-
rm -f $test_dir/config_db.json
9+
if [[ $? != 0 ]]; then
10+
echo TEST $i FAILED
11+
rm -f $(pwd)/tests/$i/config_db.json
1112
exit 1
1213
fi
1314

14-
if [[ $(diff $test_dir/expected.json $test_dir/config_db.json) ]]; then
15-
echo TEST in $test_dir FAILED
16-
diff --color=always $test_dir/expected.json $test_dir/config_db.json
17-
rm $test_dir/config_db.json
18-
exit 1
19-
fi
20-
21-
rm -f $test_dir/config_db.json
22-
}
23-
24-
test $(pwd)/tests/1
25-
test $(pwd)/tests/2
26-
test $(pwd)/tests/3
15+
echo TEST $i PASSED
16+
rm -f $(pwd)/tests/$i/config_db.json
17+
done

0 commit comments

Comments
 (0)