Skip to content

Commit 3bd2270

Browse files
committed
Shareed module experiment in sdk-platform-java
langaugecontainer and message packages are to be migrated to the librarian repository.
1 parent 598de06 commit 3bd2270

File tree

3 files changed

+184
-52
lines changed

3 files changed

+184
-52
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package languagecontainer defines LanguageContainer interface and
16+
// the Run function to execute commands within the container.
17+
// TODO(b/447404382): Move this package to the https://github.com/googleapis/librarian
18+
// GitHub repository once the interface is finalized.
19+
// This package must not have any Java-specific implementation.
20+
package languagecontainer
21+
22+
import (
23+
"context"
24+
"log/slog"
25+
26+
"cloud.google.com/java/internal/librariangen/message"
27+
)
28+
29+
// GenerateCommandContext holds the context (the file system paths) for
30+
// the generate command. The createGenerateContext function creates
31+
// an instance of this by reading the command line flags and the
32+
// default values.
33+
type GenerateCommandContext struct {
34+
}
35+
type GenerateCommandEnv struct {
36+
GenerateContext *GenerateCommandContext
37+
GenerateRequest *message.GenerateRequest
38+
}
39+
40+
type ConfigureCommandContext struct {
41+
}
42+
43+
type ConfigureCommandEnv struct {
44+
ConfigureContext *ConfigureCommandContext
45+
ConfigureRequest *message.ConfigureRequest
46+
}
47+
48+
// LanguageContainer defines the interface for language-specific container operations.
49+
type LanguageContainer interface {
50+
Generate(context.Context, *GenerateCommandEnv) error
51+
Configure(context.Context, *ConfigureCommandEnv) (*message.ConfigureResponse, error)
52+
// Other container functions like ReleaseInit and Build would also be part of the interface.
53+
}
54+
55+
// Run would accept an implementation of the LanguageContainer interface.
56+
func Run(args []string, container LanguageContainer) int {
57+
// Logic to parse args and call the appropriate method on the container.
58+
// For example, if args[1] is "generate":
59+
// request := ... // unmarshal the request from the expected location
60+
// err := container.Generate(context.Background(), request)
61+
// ...
62+
if len(args) < 1 {
63+
panic("args must not be empty")
64+
}
65+
switch args[0] {
66+
case "generate":
67+
env := createGenerateEnv(args[1:])
68+
err := container.Generate(context.Background(), env)
69+
if err != nil {
70+
// TODO: Save it as a response file.
71+
panic(err)
72+
}
73+
return 0
74+
case "configure":
75+
env := createConfigureEnv(args[1:])
76+
resp, err := container.Configure(context.Background(), env)
77+
if err != nil {
78+
panic(err)
79+
}
80+
// TODO: Save it as a response file.
81+
_ = resp
82+
return 0
83+
case "release-init":
84+
slog.Warn("librariangen: release-init command is not yet implemented")
85+
return 1
86+
case "build":
87+
slog.Warn("librariangen: build command is not yet implemented")
88+
return 1
89+
default:
90+
slog.Error("librariangen: unknown command: %s (with flags %v)", args[0], args)
91+
return 1
92+
}
93+
return 0
94+
}
95+
96+
// https://github.com/googleapis/librarian/blob/main/doc/language-onboarding.md#generate
97+
func createGenerateEnv(args []string) *GenerateCommandEnv {
98+
generateContext := createGenerateCommandContext(args)
99+
return &GenerateCommandEnv{
100+
GenerateContext: generateContext,
101+
}
102+
}
103+
104+
func createGenerateCommandContext(args []string) *GenerateCommandContext {
105+
// TODO: Parse args and create the context.
106+
return &GenerateCommandContext{}
107+
}
108+
109+
func createConfigureCommandContext(args []string) *ConfigureCommandContext {
110+
// TODO: Parse args and create the context.
111+
return &ConfigureCommandContext{}
112+
}
113+
114+
func createConfigureEnv(args []string) *ConfigureCommandEnv {
115+
configureContext := createConfigureCommandContext(args)
116+
return &ConfigureCommandEnv{
117+
ConfigureContext: configureContext,
118+
}
119+
}

internal/librariangen/main.go

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,42 @@ package main
1616

1717
import (
1818
"context"
19-
"errors"
20-
"fmt"
2119
"log/slog"
2220
"os"
23-
"strings"
21+
22+
"cloud.google.com/java/internal/librariangen/languagecontainer"
23+
"cloud.google.com/java/internal/librariangen/message"
2424
)
2525

2626
const version = "0.1.0"
2727

28-
// main is the entrypoint for the librariangen CLI.
29-
func main() {
30-
os.Exit(runCLI(os.Args))
28+
// javaContainer implements the LanguageContainer interface for Java.
29+
type javaContainer struct{}
30+
31+
func (c *javaContainer) Generate(context.Context, *languagecontainer.GenerateCommandEnv) error {
32+
// Java-specific implementation for the "generate" command.
33+
slog.Warn("librariangen: generate command is not yet implemented")
34+
35+
return nil
3136
}
3237

33-
func runCLI(args []string) int {
38+
func (c *javaContainer) Configure(ctx context.Context,
39+
request *languagecontainer.ConfigureCommandEnv) (*message.ConfigureResponse, error) {
40+
// Java-specific implementation for the "configure" command.
41+
slog.Warn("librariangen: configure command is not yet implemented")
42+
return nil, nil
43+
}
44+
45+
// main is the entrypoint for the librariangen CLI.
46+
func main() {
3447
logLevel := parseLogLevel(os.Getenv("GOOGLE_SDK_JAVA_LOGGING_LEVEL"))
3548
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
3649
Level: logLevel,
3750
})))
51+
args := os.Args
3852
slog.Info("librariangen: invoked", "args", args)
39-
if err := run(context.Background(), args[1:]); err != nil {
40-
slog.Error("librariangen: failed", "error", err)
41-
return 1
42-
}
43-
slog.Info("librariangen: finished successfully")
44-
return 0
53+
container := javaContainer{}
54+
os.Exit(languagecontainer.Run(os.Args, &container))
4555
}
4656

4757
func parseLogLevel(logLevelEnv string) slog.Level {
@@ -54,42 +64,3 @@ func parseLogLevel(logLevelEnv string) slog.Level {
5464
return slog.LevelInfo
5565
}
5666
}
57-
58-
// run executes the appropriate command based on the CLI's invocation arguments.
59-
// The idiomatic structure is `librariangen [command] [flags]`.
60-
func run(ctx context.Context, args []string) error {
61-
if len(args) < 1 {
62-
return errors.New("librariangen: expected a command")
63-
}
64-
65-
// The --version flag is a special case and not a command.
66-
if args[0] == "--version" {
67-
fmt.Println(version)
68-
return nil
69-
}
70-
71-
cmd := args[0]
72-
flags := args[1:]
73-
74-
if strings.HasPrefix(cmd, "-") {
75-
return fmt.Errorf("librariangen: command cannot be a flag: %s", cmd)
76-
}
77-
78-
switch cmd {
79-
case "generate":
80-
slog.Warn("librariangen: generate command is not yet implemented")
81-
return nil
82-
case "release-init":
83-
slog.Warn("librariangen: release-init command is not yet implemented")
84-
return nil
85-
case "configure":
86-
slog.Warn("librariangen: configure command is not yet implemented")
87-
return nil
88-
case "build":
89-
slog.Warn("librariangen: build command is not yet implemented")
90-
return nil
91-
default:
92-
return fmt.Errorf("librariangen: unknown command: %s (with flags %s)", cmd, flags)
93-
}
94-
95-
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package message defines data types which the Librarian CLI and language
16+
// containers exchange.
17+
// There shouldn't be CLI-specific data type or language container-specific
18+
// data types in this package.
19+
// TODO(b/447404382): Move this package to the https://github.com/googleapis/librarian
20+
// GitHub repository once the interface is finalized.
21+
// This package must not have any Java-specific implementation.
22+
package message
23+
24+
// GenerateRequest is the JSON message sent to the language container by
25+
// the Librarian CLI when the "generate" command is invoked.
26+
type GenerateRequest struct {
27+
}
28+
29+
// GenerateResponse is the JSON message sent back to the Librarian CLI
30+
// by the language container after processing the "generate" command.
31+
type GenerateResponse struct {
32+
}
33+
34+
// ConfigureRequest is the JSON message sent to the language container by
35+
// the Librarian CLI when the "configure" command is invoked.
36+
type ConfigureRequest struct {
37+
}
38+
39+
// ConfigureResponse is the JSON message sent back to the Librarian CLI
40+
// by the language container after processing the "configure" command.
41+
type ConfigureResponse struct {
42+
}

0 commit comments

Comments
 (0)