|
| 1 | +/** |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2023 API Testing Authors. |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +package cmd |
| 26 | + |
| 27 | +import ( |
| 28 | + "fmt" |
| 29 | + "net" |
| 30 | + |
| 31 | + "github.com/linuxsuren/api-testing/extensions/store-git/pkg" |
| 32 | + "github.com/linuxsuren/api-testing/pkg/testing/remote" |
| 33 | + "github.com/spf13/cobra" |
| 34 | + "google.golang.org/grpc" |
| 35 | +) |
| 36 | + |
| 37 | +// NewRootCommand returns the root Command |
| 38 | +func NewRootCommand() (c *cobra.Command) { |
| 39 | + opt := &options{} |
| 40 | + c = &cobra.Command{ |
| 41 | + Use: "atest-store-git", |
| 42 | + Short: "A store extension for git", |
| 43 | + RunE: opt.runE, |
| 44 | + } |
| 45 | + flags := c.Flags() |
| 46 | + flags.IntVarP(&opt.port, "port", "p", 7074, "The port to listen on") |
| 47 | + return |
| 48 | +} |
| 49 | + |
| 50 | +type options struct { |
| 51 | + port int |
| 52 | +} |
| 53 | + |
| 54 | +func (o *options) runE(c *cobra.Command, args []string) (err error) { |
| 55 | + removeServer := pkg.NewRemoteServer() |
| 56 | + |
| 57 | + var lis net.Listener |
| 58 | + lis, err = net.Listen("tcp", fmt.Sprintf(":%d", o.port)) |
| 59 | + if err != nil { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + gRPCServer := grpc.NewServer() |
| 64 | + remote.RegisterLoaderServer(gRPCServer, removeServer) |
| 65 | + c.Println("Git storage extension is running at port", o.port) |
| 66 | + |
| 67 | + go func() { |
| 68 | + <-c.Context().Done() |
| 69 | + gRPCServer.Stop() |
| 70 | + }() |
| 71 | + |
| 72 | + err = gRPCServer.Serve(lis) |
| 73 | + return |
| 74 | +} |
0 commit comments