-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl: cmd/generate: add inital command interface
Add initial command interface for generator.
- Loading branch information
Showing
5 changed files
with
164 additions
and
0 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,30 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/googleapis/generator/internal/generate" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
if err := generate.Run(ctx, os.Args[1:]...); err != nil { | ||
log.Fatal(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package generate | ||
|
||
import "errors" | ||
|
||
var errNotImplemented = errors.New("generator: functionality not implemented for language") |
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,87 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package generate | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
type config struct { | ||
googleapis string | ||
language string | ||
output string | ||
} | ||
|
||
func parseFlags(cfg *config, args []string) (*config, error) { | ||
flags := flag.NewFlagSet("", flag.ContinueOnError) | ||
flags.StringVar(&cfg.language, "language", "", "specify from cpp, csharp, go, java, node, php, python, ruby, rust") | ||
flags.StringVar(&cfg.googleapis, "googleapis", "", "location of googleapis `dir`") | ||
flags.StringVar(&cfg.output, "output", "", "location of generated client library output") | ||
|
||
// We don't want to print the whole usage message on each flags | ||
// error, so we set to a no-op and do the printing ourselves. | ||
flags.Usage = func() {} | ||
usage := func() { | ||
fmt.Fprint(flags.Output(), `Generator generates client libraries for Google APIs. | ||
Usage: | ||
generator [command] [flags] | ||
Flags: | ||
`) | ||
flags.PrintDefaults() | ||
fmt.Fprintf(flags.Output(), "\n\n") | ||
} | ||
|
||
if err := flags.Parse(args); err != nil { | ||
if err == flag.ErrHelp { | ||
usage() // print usage only on help | ||
} | ||
return nil, err | ||
} | ||
if err := validateConfig(cfg); err != nil { | ||
usage() // print usage only on help | ||
return nil, err | ||
} | ||
return cfg, nil | ||
} | ||
|
||
func validateConfig(cfg *config) error { | ||
switch cfg.language { | ||
case "cpp": | ||
return errNotImplemented | ||
case "csharp": | ||
return errNotImplemented | ||
case "go": | ||
return errNotImplemented | ||
case "java": | ||
return errNotImplemented | ||
case "node": | ||
return errNotImplemented | ||
case "php": | ||
return errNotImplemented | ||
case "python": | ||
return errNotImplemented | ||
case "ruby": | ||
return errNotImplemented | ||
case "rust": | ||
return errNotImplemented | ||
default: | ||
return fmt.Errorf("invalid -language flag specified: %q", cfg.language) | ||
} | ||
} |
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,28 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package generate | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func Run(ctx context.Context, arg ...string) error { | ||
cfg := &config{} | ||
_, err := parseFlags(cfg, arg) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |