diff --git a/cmd/humanlog/environment.go b/cmd/humanlog/environment.go index ff5b5af..a9d0ae7 100644 --- a/cmd/humanlog/environment.go +++ b/cmd/humanlog/environment.go @@ -99,7 +99,7 @@ func environmentCmd( auth.Interceptors(ll, tokenSource)..., ) orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts) - iter := ListEnvironments(ctx, orgID, orgClient) + iter := ListEnvironments(ctx, orgClient) a, ok, err := iterapi.Find(iter, func(el *organizationv1.ListEnvironmentResponse_ListItem) bool { return el.Environment.Id == environmentID }) @@ -126,7 +126,7 @@ func environmentCmd( apiURL := getAPIUrl(cctx) httpClient := getHTTPClient(cctx, apiURL) - orgID, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient) + _, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient) if err != nil { return err } @@ -136,9 +136,7 @@ func environmentCmd( ) orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts) - res, err := orgClient.CreateEnvironment(ctx, connect.NewRequest(&organizationv1.CreateEnvironmentRequest{ - OrganizationId: orgID, - })) + res, err := orgClient.CreateEnvironment(ctx, connect.NewRequest(&organizationv1.CreateEnvironmentRequest{})) if err != nil { return err } @@ -165,7 +163,7 @@ func environmentCmd( return cli.ShowSubcommandHelp(cctx) } - orgID, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient) + _, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient) if err != nil { return err } @@ -175,7 +173,7 @@ func environmentCmd( ) orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts) - el, ok, err := iterapi.Find(ListEnvironments(ctx, orgID, orgClient), func(el *organizationv1.ListEnvironmentResponse_ListItem) bool { + el, ok, err := iterapi.Find(ListEnvironments(ctx, orgClient), func(el *organizationv1.ListEnvironmentResponse_ListItem) bool { return el.Environment.Name == environmentName }) if err != nil { @@ -204,12 +202,12 @@ func environmentCmd( ) orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts) - orgID, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient) + _, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient) if err != nil { return err } - iter := ListEnvironments(ctx, orgID, orgClient) + iter := ListEnvironments(ctx, orgClient) for iter.Next() { li := iter.Current() diff --git a/cmd/humanlog/helper.go b/cmd/humanlog/helper.go index ee16967..4e5c328 100644 --- a/cmd/humanlog/helper.go +++ b/cmd/humanlog/helper.go @@ -244,7 +244,7 @@ func ensureEnvironmentSelected( var options []huh.Option[*typesv1.Environment] client := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts) - iter := ListEnvironments(ctx, orgID, client) + iter := ListEnvironments(ctx, client) for iter.Next() { item := iter.Current().Environment options = append(options, huh.NewOption(item.Name, item)) @@ -296,12 +296,11 @@ func ListOrganizations(ctx context.Context, client userv1connect.UserServiceClie }) } -func ListOrgUser(ctx context.Context, orgID int64, client organizationv1connect.OrganizationServiceClient) *iterapi.Iter[*organizationv1.ListUserResponse_ListItem] { +func ListOrgUser(ctx context.Context, client organizationv1connect.OrganizationServiceClient) *iterapi.Iter[*organizationv1.ListUserResponse_ListItem] { return iterapi.New(ctx, 100, func(ctx context.Context, cursor *typesv1.Cursor, limit int32) ([]*organizationv1.ListUserResponse_ListItem, *typesv1.Cursor, error) { list, err := client.ListUser(ctx, connect.NewRequest(&organizationv1.ListUserRequest{ - Cursor: cursor, - Limit: limit, - OrganizationId: orgID, + Cursor: cursor, + Limit: limit, })) if err != nil { return nil, nil, err @@ -310,12 +309,11 @@ func ListOrgUser(ctx context.Context, orgID int64, client organizationv1connect. }) } -func ListEnvironments(ctx context.Context, orgID int64, client organizationv1connect.OrganizationServiceClient) *iterapi.Iter[*organizationv1.ListEnvironmentResponse_ListItem] { +func ListEnvironments(ctx context.Context, client organizationv1connect.OrganizationServiceClient) *iterapi.Iter[*organizationv1.ListEnvironmentResponse_ListItem] { return iterapi.New(ctx, 100, func(ctx context.Context, cursor *typesv1.Cursor, limit int32) ([]*organizationv1.ListEnvironmentResponse_ListItem, *typesv1.Cursor, error) { list, err := client.ListEnvironment(ctx, connect.NewRequest(&organizationv1.ListEnvironmentRequest{ - Cursor: cursor, - Limit: limit, - OrganizationId: orgID, + Cursor: cursor, + Limit: limit, })) if err != nil { return nil, nil, err diff --git a/cmd/humanlog/localhost.go b/cmd/humanlog/localhost.go index 0323414..57507cf 100644 --- a/cmd/humanlog/localhost.go +++ b/cmd/humanlog/localhost.go @@ -12,8 +12,10 @@ import ( "sync" "time" + "connectrpc.com/connect" connectcors "connectrpc.com/cors" + "github.com/humanlogio/api/go/svc/feature/v1/featurev1connect" "github.com/humanlogio/api/go/svc/ingest/v1/ingestv1connect" "github.com/humanlogio/api/go/svc/localhost/v1/localhostv1connect" "github.com/humanlogio/api/go/svc/query/v1/queryv1connect" @@ -43,6 +45,9 @@ func startLocalhostServer( machineID uint64, port int, localhostHttpClient *http.Client, + apiHttpClient *http.Client, + apiURL string, + apiClientOpts []connect.ClientOption, ownVersion *typesv1.Version, ) (localsink sink.Sink, done func(context.Context) error, err error) { @@ -74,11 +79,14 @@ func startLocalhostServer( }, nil } + features := featurev1connect.NewFeatureServiceClient(apiHttpClient, apiURL, apiClientOpts...) + storage, err := localstorage.Open( ctx, cfg.ExperimentalFeatures.ServeLocalhost.Engine, ll.WithGroup("storage"), cfg.ExperimentalFeatures.ServeLocalhost.Cfg, + features, ) if err != nil { return nil, nil, fmt.Errorf("opening localstorage %q: %v", cfg.ExperimentalFeatures.ServeLocalhost.Engine, err) diff --git a/cmd/humanlog/main.go b/cmd/humanlog/main.go index 9d4758c..fc7670e 100644 --- a/cmd/humanlog/main.go +++ b/cmd/humanlog/main.go @@ -533,8 +533,17 @@ func newApp() *cli.App { return fmt.Errorf("this feature requires a valid machine ID, which requires an environment. failed to login: %v", err) } } + clOpts := []connect.ClientOption{connect.WithInterceptors( + auth.Interceptors(ll, getTokenSource(cctx))..., + )} + machineID = uint64(*state.MachineID) - localhostSink, done, err := startLocalhostServer(ctx, ll, cfg, state, machineID, localhostCfg.Port, getLocalhostHTTPClient(cctx), version) + localhostSink, done, err := startLocalhostServer( + ctx, ll, cfg, state, machineID, localhostCfg.Port, + getLocalhostHTTPClient(cctx), + getHTTPClient(cctx, getAPIUrl(cctx)), getAPIUrl(cctx), clOpts, + version, + ) if err != nil { loginfo("starting experimental localhost service: %v", err) } else { diff --git a/cmd/humanlog/organization.go b/cmd/humanlog/organization.go index c337d36..870999d 100644 --- a/cmd/humanlog/organization.go +++ b/cmd/humanlog/organization.go @@ -274,21 +274,15 @@ func organizationCmd( Action: func(cctx *cli.Context) error { ctx := getCtx(cctx) ll := getLogger(cctx) - state := getState(cctx) tokenSource := getTokenSource(cctx) apiURL := getAPIUrl(cctx) httpClient := getHTTPClient(cctx, apiURL) - orgID, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient) - if err != nil { - return err - } - clOpts := connect.WithInterceptors( auth.Interceptors(ll, tokenSource)..., ) organizationClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts) - iter := ListOrgUser(ctx, orgID, organizationClient) + iter := ListOrgUser(ctx, organizationClient) for iter.Next() { u := iter.Current().User printFact("id", u.Id) diff --git a/go.mod b/go.mod index 4b516c6..20402f9 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 github.com/go-logfmt/logfmt v0.5.1 github.com/google/uuid v1.6.0 - github.com/humanlogio/api/go v0.0.0-20241113101248-3e03dc2ef4e0 + github.com/humanlogio/api/go v0.0.0-20241128170213-590d167300cd github.com/humanlogio/humanlog-pro v0.0.0-20241029032705-0ce172ecfc87 github.com/kr/logfmt v0.0.0-20210122060352-19f9bcb100e6 github.com/lrstanley/bubblezone v0.0.0-20240914071701-b48c55a5e78e diff --git a/go.sum b/go.sum index c3c5f56..284cb75 100644 --- a/go.sum +++ b/go.sum @@ -82,6 +82,8 @@ github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8 github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0= github.com/humanlogio/api/go v0.0.0-20241113101248-3e03dc2ef4e0 h1:NFmmOmTIlhNCWQX7TLKQtr6xkdOvesoygcqWruCBx2w= github.com/humanlogio/api/go v0.0.0-20241113101248-3e03dc2ef4e0/go.mod h1:+hU/MU1g6QvtbeknKOlUI1yEStVqkPJ8jmYIj63OV5I= +github.com/humanlogio/api/go v0.0.0-20241128170213-590d167300cd h1:449C6cnB4W6DblDMPfCfA4xyEkiYMpngGf7TEX9O8ro= +github.com/humanlogio/api/go v0.0.0-20241128170213-590d167300cd/go.mod h1:+hU/MU1g6QvtbeknKOlUI1yEStVqkPJ8jmYIj63OV5I= github.com/humanlogio/humanlog-pro v0.0.0-20241029032705-0ce172ecfc87 h1:z6x3cbdSyvnlmiuQ0v3Qi2iuJoRh1dyKydHIFy4J0is= github.com/humanlogio/humanlog-pro v0.0.0-20241029032705-0ce172ecfc87/go.mod h1:1aHRO1O8oH4Mht2hOAo+kiLalHNu1SpFDcvuyauZuos= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= diff --git a/internal/memstorage/memory.go b/internal/memstorage/memory.go index b1cd469..90e33fd 100644 --- a/internal/memstorage/memory.go +++ b/internal/memstorage/memory.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "github.com/humanlogio/api/go/svc/feature/v1/featurev1connect" typesv1 "github.com/humanlogio/api/go/types/v1" "github.com/humanlogio/humanlog/pkg/localstorage" "github.com/humanlogio/humanlog/pkg/sink" @@ -23,7 +24,7 @@ var ( ) func init() { - localstorage.RegisterStorage("basic", func(ctx context.Context, ll *slog.Logger, cfg map[string]interface{}) (localstorage.Storage, error) { + localstorage.RegisterStorage("basic", func(ctx context.Context, ll *slog.Logger, cfg map[string]interface{}, features featurev1connect.FeatureServiceClient) (localstorage.Storage, error) { return NewMemStorage(ll), nil }) } diff --git a/pkg/localstorage/queryable.go b/pkg/localstorage/queryable.go index f1b16f7..50968c9 100644 --- a/pkg/localstorage/queryable.go +++ b/pkg/localstorage/queryable.go @@ -6,11 +6,17 @@ import ( "log/slog" "time" + "github.com/humanlogio/api/go/svc/feature/v1/featurev1connect" typesv1 "github.com/humanlogio/api/go/types/v1" "github.com/humanlogio/humanlog/pkg/sink" ) -type StorageBuilder func(ctx context.Context, ll *slog.Logger, cfg map[string]interface{}) (Storage, error) +type StorageBuilder func( + ctx context.Context, + ll *slog.Logger, + cfg map[string]interface{}, + features featurev1connect.FeatureServiceClient, +) (Storage, error) var registry = make(map[string]StorageBuilder) @@ -22,12 +28,12 @@ func RegisterStorage(name string, builder StorageBuilder) { registry[name] = builder } -func Open(ctx context.Context, name string, ll *slog.Logger, cfg map[string]interface{}) (Storage, error) { +func Open(ctx context.Context, name string, ll *slog.Logger, cfg map[string]interface{}, features featurev1connect.FeatureServiceClient) (Storage, error) { builder, ok := registry[name] if !ok { return nil, fmt.Errorf("no storage engine with name %q", name) } - return builder(ctx, ll, cfg) + return builder(ctx, ll, cfg, features) } type Storage interface { diff --git a/pkg/tui/accounts.go b/pkg/tui/accounts.go index a77412b..bc5bd01 100644 --- a/pkg/tui/accounts.go +++ b/pkg/tui/accounts.go @@ -165,9 +165,8 @@ func listEnvironmentsCmd( return func() tea.Msg { log.Printf("environment: listEnvironments") res, err := organizationClient.ListEnvironment(ctx, connect.NewRequest(&organizationv1.ListEnvironmentRequest{ - Cursor: nil, - Limit: 10, - OrganizationId: *state.CurrentOrgID, + Cursor: nil, + Limit: 10, })) if err != nil { cerr := new(connect.Error)