-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to get and set data in files of specific formats (#492)
* SNOW-1665681 Add set/get operators
- Loading branch information
1 parent
39af558
commit 6817e07
Showing
37 changed files
with
4,496 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Local file | ||
Service to manipulate file on remote machines | ||
|
||
# Usage | ||
|
||
### sanssh file get-data | ||
Get data from a file of specific format on a remote host by specified data key. | ||
|
||
```bash | ||
sanssh <sanssh-args> file get-data [--format <file-format>] <file-path> <data-key> | ||
``` | ||
Where: | ||
- `<sanssh-args>` common sanssh arguments | ||
- `<file-path>` is the path to the file on remote machine. If --format is not provided, format would be detected from file extension. | ||
- `<data-key>` is the key to read from the file. For different file formats it would require keys in different format | ||
- for `yml`, key should be valid [YAMLPath](https://github.com/goccy/go-yaml/tree/master?tab=readme-ov-file#5-use-yamlpath) string | ||
- for `dotenv`, key should be a name of variable | ||
- `<file-format>` is the format of the file, if specified it would override the format detected from file extension. Supported formats are: | ||
- `yml` | ||
- `dotenv` | ||
|
||
Examples: | ||
```bash | ||
# Get data from a yml file | ||
sanssh --targets $TARGET file get-data /etc/config.yml "$.databases[0].host" | ||
# Get data from a dotenv with explicitly specified format | ||
sanssh --targets file get-data --format dotenv /etc/some-config "HOST" | ||
``` | ||
|
||
### sanssh file set-data | ||
Set data to a file of specific format on a remote host by specified data key. | ||
|
||
```bash | ||
sanssh <sanssh-args> file set-data [--format <file-format>] [--value-type <value-type>] <file-path> <data-key> <value> | ||
``` | ||
Where: | ||
- `<sanssh-args>` common sanssh arguments | ||
- `<file-path>` is the path to the file on remote machine. If --format is not provided, format would be detected from file extension. | ||
- `<data-key>` is the key to set value in the file. For different file formats it would require keys in different format | ||
- for `yml`, key should be valid [YAMLPath](https://github.com/goccy/go-yaml/tree/master?tab=readme-ov-file#5-use-yamlpath) string | ||
- for `dotenv`, key should be a name of variable | ||
- `<value>` is the value to set in the file | ||
- `<file-format>` is the format of the file, if specified it would override the format detected from file extension. Supported formats are: | ||
- `yml` | ||
- `dotenv` | ||
- `<value-type>` is the type of value to set in the file. By default, `string`. Supported types are: | ||
- `string` | ||
- `int` | ||
- `float` | ||
- `bool` | ||
|
||
Examples: | ||
```bash | ||
# Set data to a yml file | ||
sanssh --targets $TARGET file set-data /etc/config.yml "database.host" "localhost" | ||
# Set data to a dotenv with explicitly specified format | ||
sanssh --targets file set-data --format dotenv /etc/some-config "HOST" "localhost" | ||
# Set data specified type | ||
sanssh --targets file set-data --value-type int /etc/config.yml "database.port" 8080 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright (c) 2024 Snowflake Inc. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
*/ | ||
|
||
package cli_controllers | ||
|
||
import ( | ||
"errors" | ||
pb "github.com/Snowflake-Labs/sansshell/services/localfile" | ||
"path/filepath" | ||
) | ||
|
||
func getFileTypeFromPath(filePath string) (pb.FileFormat, error) { | ||
fileExt := filepath.Ext(filePath) | ||
|
||
switch fileExt { | ||
case ".yml", ".yaml": | ||
return pb.FileFormat_YML, nil | ||
case ".env": | ||
return pb.FileFormat_DOTENV, nil | ||
default: | ||
return pb.FileFormat_UNKNOWN, errors.New("file type is unsupported") | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
services/localfile/client/cli-controllers/get-data.controller.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* Copyright (c) 2024 Snowflake Inc. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
*/ | ||
|
||
package cli_controllers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
pb "github.com/Snowflake-Labs/sansshell/services/localfile" | ||
"github.com/Snowflake-Labs/sansshell/services/util" | ||
cliUtils "github.com/Snowflake-Labs/sansshell/services/util/cli" | ||
"github.com/google/subcommands" | ||
"google.golang.org/grpc/status" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// setDataCmd cli adapter for execution infrastructure implementation of [subcommands.Command] interface | ||
type getDataCmd struct { | ||
fileFormat pb.FileFormat | ||
cliLogger cliUtils.StyledCliLogger | ||
} | ||
|
||
func (*getDataCmd) Name() string { return "get-data" } | ||
func (*getDataCmd) Synopsis() string { | ||
return "Get data from file of specific format. Currently supported: yml, dotenv" | ||
} | ||
func (*getDataCmd) Usage() string { | ||
return `get-data [--format=yml|dotenv] <file-path> <data-key>: | ||
Get value by 'data-key' from file of file by 'file-path' of specific format. | ||
Arguments: | ||
- file-path - path to file with data | ||
- data-key - key to read data from file. For different file format it should be: | ||
- yml - YmlPath string | ||
- dotenv - variable key | ||
Format could be detected from file extension or explicitly specified by --format flag. | ||
Flags: | ||
` | ||
} | ||
|
||
func (p *getDataCmd) SetFlags(f *flag.FlagSet) { | ||
f.Func("format", "File format (Optional). Could be one of: yml, dotenv", func(s string) error { | ||
lowerCased := strings.ToLower(s) | ||
|
||
switch lowerCased { | ||
case "yml": | ||
p.fileFormat = pb.FileFormat_YML | ||
case "dotenv": | ||
p.fileFormat = pb.FileFormat_DOTENV | ||
default: | ||
return errors.New("could be only yml or dotenv") | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
// Execute is a method handle command execution. It adapter between cli and business logic | ||
func (p *getDataCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus { | ||
state := args[0].(*util.ExecuteState) | ||
|
||
if len(f.Args()) < 1 { | ||
p.cliLogger.Errorc(cliUtils.RedText, "File path is missing.\n") | ||
return subcommands.ExitUsageError | ||
} | ||
|
||
if len(f.Args()) < 2 { | ||
p.cliLogger.Errorc(cliUtils.RedText, "Property path is missing.\n") | ||
return subcommands.ExitUsageError | ||
} | ||
|
||
remoteFilePath := f.Arg(0) | ||
dataKey := f.Arg(1) | ||
|
||
fileFormat := p.fileFormat | ||
if fileFormat == pb.FileFormat_UNKNOWN { | ||
fileFormatFromExt, err := getFileTypeFromPath(remoteFilePath) | ||
if err != nil { | ||
p.cliLogger.Errorfc(cliUtils.RedText, "Could not get file type from filepath: %s\n", err.Error()) | ||
return subcommands.ExitUsageError | ||
} | ||
|
||
fileFormat = fileFormatFromExt | ||
} | ||
|
||
preloader := cliUtils.NewDotPreloader("Waiting for results from remote machines", util.IsStreamToTerminal(os.Stdout)) | ||
client := pb.NewLocalFileClientProxy(state.Conn) | ||
|
||
preloader.Start() | ||
responses, err := client.DataGetOneMany(ctx, &pb.DataGetRequest{ | ||
Filename: remoteFilePath, | ||
DataKey: dataKey, | ||
FileFormat: fileFormat, | ||
}) | ||
|
||
if err != nil { | ||
preloader.Stop() | ||
p.cliLogger.Errorfc(cliUtils.RedText, "Unexpected error: %s\n", err.Error()) | ||
return subcommands.ExitFailure | ||
} | ||
|
||
for resp := range responses { | ||
preloader.Stop() | ||
|
||
targetLogger := cliUtils.NewStyledCliLogger(state.Out[resp.Index], state.Err[resp.Index], &cliUtils.CliLoggerOptions{ | ||
ApplyStylingForErr: util.IsStreamToTerminal(state.Err[resp.Index]), | ||
ApplyStylingForOut: util.IsStreamToTerminal(state.Out[resp.Index]), | ||
}) | ||
|
||
if resp.Error != nil { | ||
st, _ := status.FromError(resp.Error) | ||
targetLogger.Errorfc(cliUtils.RedText, | ||
"Failed to get value: %s\n", | ||
st.Message(), | ||
) | ||
continue | ||
} | ||
targetLogger.Infof("Value: %s\n", resp.Resp.Value) | ||
|
||
preloader.Start() | ||
} | ||
preloader.StopWith("Completed.\n") | ||
|
||
return subcommands.ExitSuccess | ||
} | ||
|
||
func NewDataGetCmd() subcommands.Command { | ||
return &getDataCmd{ | ||
fileFormat: pb.FileFormat_UNKNOWN, | ||
cliLogger: cliUtils.NewStyledCliLogger(os.Stdout, os.Stderr, &cliUtils.CliLoggerOptions{ | ||
ApplyStylingForErr: util.IsStreamToTerminal(os.Stderr), | ||
ApplyStylingForOut: util.IsStreamToTerminal(os.Stdout), | ||
}), | ||
} | ||
} |
Oops, something went wrong.