-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Define stdout and stderr per job * Add streaming api response for mittnitectl logs * use url instead of string * add unfinished follow function for job logs * always follow logs * fix log error * mittnitectl breaking: change job syntax + add logs and list subcommand * add --tail arg for job logs * fix formatting and add help for `mittnitectl job` * update to go 1.19 * add docs for stdout/err options and fix typo * make build methods more readable * set default value for `--tail` to -1 + make it possible to set `--tail` to "0" for only new log outputs
- Loading branch information
Showing
19 changed files
with
671 additions
and
117 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
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 |
---|---|---|
@@ -1,44 +1,15 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/mittwald/mittnite/pkg/cli" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
ctlCommand.AddCommand(jobCommand) | ||
jobCommand.SetHelpTemplate(`{{.Long}} | ||
Usage: | ||
{{.UseLine}} | ||
Arguments: | ||
name: the name of the job | ||
action: possible values are "start", "restart", "stop" and "status" | ||
Flags: | ||
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}} | ||
Global Flags: | ||
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}} | ||
`) | ||
} | ||
|
||
var jobCommand = &cobra.Command{ | ||
Use: "job <name> <action>", | ||
Args: cobra.ExactArgs(2), | ||
ArgAliases: []string{"name", "action"}, | ||
Short: "Control a job via command line", | ||
Long: "This command can be used to control a job managed by mittnite.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
job := args[0] | ||
action := args[1] | ||
apiClient := cli.NewApiClient(apiAddress) | ||
|
||
resp := apiClient.CallAction(job, action) | ||
if err := resp.Print(); err != nil { | ||
log.Errorf("failed to print output: %s", err.Error()) | ||
} | ||
}, | ||
Use: "job", | ||
Short: "Control a job via command line", | ||
Long: "This command can be used to control a managed job.", | ||
} |
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,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/mittwald/mittnite/pkg/cli" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
jobCommand.AddCommand(buildJobActionCommand("start", "Start a job", "This command can be used to start a managed job.")) | ||
jobCommand.AddCommand(buildJobActionCommand("restart", "Restart a job", "This command can be used to restart a managed job.")) | ||
jobCommand.AddCommand(buildJobActionCommand("stop", "Stop a job", "This command can be used to stop a managed job.")) | ||
jobCommand.AddCommand(buildJobActionCommand("status", "Show job status", "This command can be used to show the status of a managed job.")) | ||
} | ||
|
||
func buildJobActionCommand(action string, shortDesc, longDesc string) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: fmt.Sprintf("%s <job>", action), | ||
Args: cobra.ExactArgs(1), | ||
ArgAliases: []string{"job"}, | ||
Short: shortDesc, | ||
Long: longDesc, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
job := args[0] | ||
apiClient := cli.NewApiClient(apiAddress) | ||
|
||
resp := apiClient.CallAction(job, action) | ||
if err := resp.Print(); err != nil { | ||
log.Errorf("failed to print output: %s", err.Error()) | ||
} | ||
}, | ||
} | ||
} |
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,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/mittwald/mittnite/pkg/cli" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
jobCommand.AddCommand(jobListCommand) | ||
} | ||
|
||
var jobListCommand = &cobra.Command{ | ||
Use: "list", | ||
Short: "List jobs", | ||
Long: "This command can be used to list all managed jobs.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
apiClient := cli.NewApiClient(apiAddress) | ||
|
||
resp := apiClient.JobList() | ||
if err := resp.Print(); err != nil { | ||
log.Errorf("failed to print output: %s", err.Error()) | ||
} | ||
}, | ||
} |
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,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/mittwald/mittnite/pkg/cli" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
follow bool | ||
tailLen int | ||
) | ||
|
||
func init() { | ||
jobLogsCommand.PersistentFlags().BoolVarP(&follow, "follow", "f", false, "output appended data as the file grows") | ||
jobLogsCommand.PersistentFlags().IntVarP(&tailLen, "tail", "", -1, "output last n lines") | ||
jobCommand.AddCommand(jobLogsCommand) | ||
} | ||
|
||
var jobLogsCommand = &cobra.Command{ | ||
Use: "logs <job>", | ||
Short: "Get logs from job", | ||
Long: "This command can be used to get the logs of a managed job.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
job := args[0] | ||
apiClient := cli.NewApiClient(apiAddress) | ||
|
||
if tailLen < -1 { | ||
tailLen = -1 | ||
} | ||
resp := apiClient.JobLogs(job, follow, tailLen) | ||
if err := resp.Print(); err != nil { | ||
log.Errorf("failed to print output: %s", err.Error()) | ||
} | ||
}, | ||
} |
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
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,52 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"github.com/gorilla/websocket" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
func (api *ApiClient) buildHTTPClientAndURL() (*http.Client, *url.URL, error) { | ||
u, err := url.Parse(api.apiAddress) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if u.Scheme != "unix" { | ||
return &http.Client{}, u, nil | ||
} | ||
|
||
socketPath := u.Path | ||
u.Scheme = "http" | ||
u.Host = "unix" | ||
return &http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", socketPath) | ||
}, | ||
}, | ||
}, u, nil | ||
} | ||
|
||
func (api *ApiClient) buildWebsocketURL() (*websocket.Dialer, *url.URL, error) { | ||
u, err := url.Parse(api.apiAddress) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if u.Scheme != "unix" { | ||
u.Scheme = "ws" | ||
return websocket.DefaultDialer, u, nil | ||
} | ||
socketPath := u.Path | ||
|
||
dialer := &websocket.Dialer{ | ||
NetDial: func(network, addr string) (net.Conn, error) { | ||
return net.Dial("unix", socketPath) | ||
}, | ||
} | ||
|
||
u.Scheme = "ws" | ||
u.Host = "unix" | ||
return dialer, u, nil | ||
} |
Oops, something went wrong.