-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c783805
commit 23e937b
Showing
8 changed files
with
205 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/weikanglim/ksd/internal/ksd" | ||
) | ||
|
||
func GetCredentialOptionsFromFlags() (ksd.CredentialOptions, error) { | ||
opts := ksd.CredentialOptions{} | ||
if clientId != "" { | ||
if clientSecret == "" { | ||
return opts, errors.New("`--client-secret` must be set when `--client-id` is provided") | ||
} | ||
|
||
if tenantId == "" { | ||
return opts, errors.New("`--tenant-id` must be set when `--client-id` is provided") | ||
} | ||
|
||
opts.ClientId = clientId | ||
opts.ClientSecret = clientSecret | ||
opts.TenantId = tenantId | ||
} else { | ||
if clientSecret != "" { | ||
return opts, errors.New("`--client-id` must be set when `--client-secret` is provided") | ||
} | ||
|
||
if tenantId != "" { | ||
return opts, errors.New("`--client-id` must be set when `--tenant-id` is provided") | ||
} | ||
} | ||
return opts, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/MakeNowJust/heredoc/v2" | ||
"github.com/spf13/cobra" | ||
"github.com/weikanglim/ksd/internal/ksd" | ||
) | ||
|
||
func NewRunCmd() *cobra.Command { | ||
var script string | ||
|
||
var runCmd = &cobra.Command{ | ||
Use: "run <file>", | ||
Short: "Runs a script file.", | ||
Args: cobra.MaximumNArgs(1), | ||
Long: heredoc.Doc(` | ||
Run executes the script file against a Kusto database. | ||
`), | ||
Example: heredoc.Doc(` | ||
# Run a script file | ||
$ ksd run ./script.ksl | ||
`), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if endpoint == "" { | ||
return errors.New("missing `--endpoint`. Set this to a Azure Data Explorer database endpoint, i.e. https://samples.kusto.windows.net/MyDatabase") | ||
} | ||
|
||
file := args[0] | ||
_, err := os.Stat(file) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("script file %s does not exist", file) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("reading %s: %w", file, err) | ||
} | ||
|
||
credOptions, err := GetCredentialOptionsFromFlags() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ksd.Run(file, endpoint, credOptions, http.DefaultClient) | ||
}, | ||
} | ||
|
||
runCmd.Flags().StringVar(&script, "script", "", "The script file to run.") | ||
|
||
// Connection flags | ||
runCmd.Flags().StringVar(&endpoint, "endpoint", "", "The endpoint to the Azure Data Explorer database") | ||
runCmd.Flags().StringVar(&clientId, "client-id", "", "The ID of the application to authenticate with") | ||
runCmd.Flags().StringVar(&clientSecret, "client-secret", "", "The secret of the application to authenticate with") | ||
runCmd.Flags().StringVar(&tenantId, "tenant-id", "", "The tenant ID of the application to authenticate with") | ||
|
||
return runCmd | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ksd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/Azure/azure-kusto-go/kusto/kql" | ||
) | ||
|
||
// Run executes a Kusto script. | ||
func Run( | ||
file string, | ||
endpoint string, | ||
cred CredentialOptions, | ||
httpClient *http.Client) error { | ||
conn, err := parseEndpoint(endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
client, err := newKustoClient(conn.endpoint, cred, httpClient) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() error { | ||
err = client.Close() | ||
if err != nil { | ||
return fmt.Errorf("closing client: %w", err) | ||
} | ||
return nil | ||
}() | ||
|
||
ctx := context.Background() | ||
cmdScript, err := os.ReadFile(file) | ||
if err != nil { | ||
return fmt.Errorf("reading file %s: %w", file, err) | ||
} | ||
query := kql.New("") | ||
query.AddUnsafe(string(cmdScript)) | ||
_, err = client.Mgmt( | ||
ctx, | ||
conn.db, | ||
query) | ||
if err != nil { | ||
return fmt.Errorf("running command: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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,42 @@ | ||
package test | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRun_Live(t *testing.T) { | ||
cfg, err := getLiveConfig() | ||
if err != nil { | ||
t.Skip(err.Error()) | ||
} | ||
|
||
runArgs := []string{"run"} | ||
runArgs = append(runArgs, argsFromConfig(cfg)...) | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
chdir string | ||
expect func(t *testing.T, r cmdResult) | ||
}{ | ||
{ | ||
"File", | ||
append(runArgs, filepath.Join("scripts", "show.csl")), | ||
"testdata", | ||
nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.chdir != "" { | ||
Chdir(t, tt.chdir) | ||
} | ||
|
||
res := executeCmd(tt.args) | ||
require.NoError(t, res.Err) | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.show functions |