Browse Source

cmd

- add `all` mode
- refactoring
Alexey Kim 2 năm trước cách đây
mục cha
commit
39764b11dc
2 tập tin đã thay đổi với 46 bổ sung37 xóa
  1. 1 4
      Makefile
  2. 45 33
      cmd/main.go

+ 1 - 4
Makefile

@@ -162,7 +162,4 @@ add-context:
 generate:
 	-@echo "-> $@"
 	-rm -rf graph
-	@go run gshopper.com/gshopify/admin-api/cmd -m customer
-	@go run gshopper.com/gshopify/admin-api/cmd -m product
-	@go run gshopper.com/gshopify/admin-api/cmd -m order
-	@go run gshopper.com/gshopify/admin-api/cmd -m shop
+	@go run gshopper.com/gshopify/admin-api/cmd -m all

+ 45 - 33
cmd/main.go

@@ -10,7 +10,6 @@ import (
 )
 
 var (
-	cfg     = config.DefaultConfig()
 	fModule = pflag.StringP("module", "m", "", "module name")
 )
 
@@ -24,62 +23,75 @@ func init() {
 }
 
 func main() {
-	module := admin_api.Module(*fModule)
-	if !module.IsValid() {
-		_, _ = fmt.Fprintf(os.Stderr, "incorrect module name has been specified; available modules are: %v\n", admin_api.AllModules)
-		os.Exit(2)
+	var modules []admin_api.Module
+	if *fModule == "all" {
+		modules = admin_api.AllModules
+	} else {
+		module := admin_api.Module(*fModule)
+		if !module.IsValid() {
+			_, _ = fmt.Fprintf(os.Stderr, "incorrect module has been specified; available modules are: %v\n", admin_api.AllModules)
+			os.Exit(2)
+		}
+
+		modules = append(modules, module)
 	}
 
-	moduleSchema := fmt.Sprintf("subgraph/%s.tpl", module)
-	f, err := os.OpenFile(moduleSchema, os.O_RDONLY, 0644)
-	if err != nil {
-		_, _ = fmt.Fprintf(os.Stderr, "unknown module; %v\n", err.Error())
-		os.Exit(2)
+	for _, module := range modules {
+		if err := generate(module); err != nil {
+			_, _ = fmt.Fprintf(os.Stderr, err.Error())
+			os.Exit(2)
+		}
 	}
-	defer f.Close()
 
-	if stat, err := f.Stat(); err != nil || stat.Size() < 1 {
-		fmt.Printf("illegal module definition schema")
-		_, _ = fmt.Fprintf(os.Stderr, "illegal module definition schema")
-		os.Exit(2)
+	os.Exit(0)
+}
+
+func generate(m admin_api.Module) error {
+	schema := fmt.Sprintf("subgraph/%s.tpl", m)
+	if stat, err := os.Stat(schema); err != nil || stat.Size() < 1 {
+		return fmt.Errorf("illegal module schema definition")
+	}
+
+	cfg := makeConfig(m)
+	if err := config.CompleteConfig(cfg); err != nil {
+		return err
+	}
+
+	if err := api.Generate(cfg); err != nil {
+		return err
 	}
 
+	return nil
+}
+
+func makeConfig(m admin_api.Module) *config.Config {
+	cfg := config.DefaultConfig()
+
 	// Federation
 	cfg.Federation = config.PackageConfig{
-		Filename: fmt.Sprintf("graph/%s/generated/federation.go", module),
+		Filename: fmt.Sprintf("graph/%s/generated/federation.go", m),
 		Version:  2,
 	}
 
 	// Exec
 	cfg.Exec = config.ExecConfig{
-		Filename: fmt.Sprintf("graph/%s/generated/generated.go", module),
+		Filename: fmt.Sprintf("graph/%s/generated/generated.go", m),
 	}
 
 	// Model
 	cfg.Model = config.PackageConfig{
-		Filename: fmt.Sprintf("graph/%s/generated/models.go", module),
+		Filename: fmt.Sprintf("graph/%s/generated/models.go", m),
 	}
 
+	// Resolver
 	cfg.Resolver = config.ResolverConfig{
 		Layout:  config.LayoutFollowSchema,
-		DirName: fmt.Sprintf("graph/%s", module),
+		DirName: fmt.Sprintf("graph/%s", m),
 	}
 
 	// SchemaFilename
 	cfg.SchemaFilename = config.StringList{
-		moduleSchema,
-		"ext/*.graphql",
-	}
-
-	if err := config.CompleteConfig(cfg); err != nil {
-		_, _ = fmt.Fprintln(os.Stderr, err.Error())
-		os.Exit(3)
-	}
-
-	if err := api.Generate(cfg); err != nil {
-		_, _ = fmt.Fprintln(os.Stderr, err.Error())
-		os.Exit(3)
-	}
+		fmt.Sprintf("subgraph/%s.tpl", m), "ext/*.graphql"}
 
-	os.Exit(0)
+	return cfg
 }