Skip to content

Commit

Permalink
subcommands to fill in later
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme committed Sep 28, 2024
1 parent 08dcdbc commit 17bfef9
Show file tree
Hide file tree
Showing 4 changed files with 471 additions and 2 deletions.
158 changes: 158 additions & 0 deletions cmd/humanlog/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package main

import (
"context"
"net/http"

"github.com/humanlogio/humanlog/internal/pkg/config"
"github.com/humanlogio/humanlog/internal/pkg/state"
"github.com/humanlogio/humanlog/pkg/auth"
"github.com/urfave/cli"
)

const (
accountCmdName = "account"
)

func accountCmd(
getCtx func(cctx *cli.Context) context.Context,
getCfg func(cctx *cli.Context) *config.Config,
getState func(cctx *cli.Context) *state.State,
getTokenSource func(cctx *cli.Context) *auth.UserRefreshableTokenSource,
getAPIUrl func(cctx *cli.Context) string,
getHTTPClient func(*cli.Context) *http.Client,
) cli.Command {
return cli.Command{
Name: accountCmdName,
Usage: "Manage accounts for the current user or org.",
Before: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)
_, err := ensureLoggedIn(ctx, cctx, state, tokenSource, apiURL, httpClient)
if err != nil {
return err
}
return nil
},
Subcommands: []cli.Command{
{
Name: "set-current",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)

accountName := cctx.Args().First()
// lookup `accountName` and set its ID in `state`
_ = ctx
_ = state
_ = tokenSource
_ = apiURL
_ = httpClient
_ = accountName

return nil
},
},
{
Name: "get-current",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)

accountName := cctx.Args().First()
// lookup `account ID` in state, and resolve to `accountName` via API
_ = ctx
_ = state
_ = tokenSource
_ = apiURL
_ = httpClient
_ = accountName

return nil
},
},
{
Name: "create",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)

_ = ctx
_ = state
_ = tokenSource
_ = apiURL
_ = httpClient

return nil
},
},
{
Name: "get",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)

_ = ctx
_ = state
_ = tokenSource
_ = apiURL
_ = httpClient

return nil
},
},
{
Name: "update",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)

_ = ctx
_ = state
_ = tokenSource
_ = apiURL
_ = httpClient

return nil
},
},
{
Name: "list",
Usage: "list the accounts for the current user or org",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)

_ = ctx
_ = state
_ = tokenSource
_ = apiURL
_ = httpClient

return nil
},
},
},
}
}
76 changes: 76 additions & 0 deletions cmd/humanlog/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"context"
"fmt"
"net/http"

"github.com/humanlogio/humanlog/internal/pkg/config"
"github.com/humanlogio/humanlog/internal/pkg/state"
"github.com/humanlogio/humanlog/pkg/auth"
"github.com/urfave/cli"
)

const (
machineCmdName = "machine"
)

func machineCmd(
getCtx func(cctx *cli.Context) context.Context,
getCfg func(cctx *cli.Context) *config.Config,
getState func(cctx *cli.Context) *state.State,
getTokenSource func(cctx *cli.Context) *auth.UserRefreshableTokenSource,
getAPIUrl func(cctx *cli.Context) string,
getHTTPClient func(*cli.Context) *http.Client,
) cli.Command {
return cli.Command{
Name: machineCmdName,
Usage: "Manage machines in the current account.",
Before: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)
_, err := ensureLoggedIn(ctx, cctx, state, tokenSource, apiURL, httpClient)
if err != nil {
return err
}
return nil
},
Subcommands: []cli.Command{
{
Name: "register",
Usage: "register this machine to save logs in account",
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx)
accountToken, err := createIngestionToken(ctx, cctx, state, tokenSource, apiURL, httpClient)
if err != nil {
return fmt.Errorf("ingestion token couldn't be generated: %v", err)
}
state.IngestionToken = accountToken
if err := state.WriteBack(); err != nil {
return fmt.Errorf("writing back generated ingestion token: %v", err)
}
return nil
},
},
{
Name: "deregister",
Usage: "deregister this machine from saving logs in account",
Action: func(cctx *cli.Context) error {
state := getState(cctx)
state.IngestionToken = nil
if err := state.WriteBack(); err != nil {
return fmt.Errorf("writing back generated ingestion token: %v", err)
}
return nil
},
},
},
}
}
18 changes: 16 additions & 2 deletions cmd/humanlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,22 @@ func newApp() *cli.App {
}

app := cli.NewApp()
app.Author = "Antoine Grondin"
app.Email = "[email protected]"
app.Author = "humanlog.io"
app.Email = "[email protected]"
app.Name = "humanlog"
app.Version = semverVersion.String()
app.Usage = "reads structured logs from stdin, makes them pretty on stdout!"
app.Description = `humanlog parses logs and makes them easier to read and search.
When invoked with no argument, it consumes stdin and parses it,
attempts to make it prettier on stdout. It also allows searching
the logs that were parsed, both in a TUI by pressing "s" or in a
webapp by pressing "space".
If registered to ingest logs via "humanlog machine register" logs
will be saved to humanlog.io for vizualization, searching and
analysis.
`

var (
ctx context.Context
Expand Down Expand Up @@ -297,6 +308,9 @@ func newApp() *cli.App {
app.Commands,
versionCmd(getCtx, getLogger, getCfg, getState, getTokenSource, getAPIUrl, getHTTPClient),
authCmd(getCtx, getCfg, getState, getTokenSource, getAPIUrl, getHTTPClient),
organizationCmd(getCtx, getCfg, getState, getTokenSource, getAPIUrl, getHTTPClient),
accountCmd(getCtx, getCfg, getState, getTokenSource, getAPIUrl, getHTTPClient),
machineCmd(getCtx, getCfg, getState, getTokenSource, getAPIUrl, getHTTPClient),
queryCmd(getCtx, getCfg, getState, getTokenSource, getAPIUrl, getHTTPClient),
gennyCmd(getCtx, getCfg, getState),
)
Expand Down
Loading

0 comments on commit 17bfef9

Please sign in to comment.