Merge changes Ic75c7eca,Ic0b547f7,I932b1ceb,I26c99032,Ifaeeb8f4, ...
[cli.git] / grpc / grpc-client / src / main / java / org / open / infc / grpc / client / OpenRemoteCli.java
1 /*
2  * Copyright 2018 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.open.infc.grpc.client;
18
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.logging.Logger;
23
24 import org.open.infc.grpc.Args;
25 import org.open.infc.grpc.Result;
26
27 public class OpenRemoteCli {
28     public static final String OCLIP_GRPC_SERVER = "http://localhost:50051";
29     public static final String OCLIP_GRPC_SERVER_ENV = "OCLIP_SERVER";
30
31     public static Result run (String[] args) throws Exception {
32         String oclipHome = System.getenv(OCLIP_GRPC_SERVER_ENV);
33
34         if (oclipHome == null) {
35             oclipHome = OCLIP_GRPC_SERVER;
36         }
37
38           if (System.getenv("OPEN_CLI_DEBUG") == null) {
39               Logger globalLogger = Logger.getLogger(OpenInterfaceGrpcClient.class.getName());
40               globalLogger.setLevel(java.util.logging.Level.OFF);
41           } else {
42               System.out.println(OCLIP_GRPC_SERVER_ENV + "=" + oclipHome);
43           }
44
45           if (args.length <= 2 || !args[0].equals("-P")) {
46               System.out.println("Usage: oclip -P <product-name> <command-name> <command-arguments");
47               System.out.println("NOTE: Set environment variable " + OCLIP_GRPC_SERVER_ENV + " to OCLIP gRPC server. By default its " + OCLIP_GRPC_SERVER);
48               System.exit(0);
49           }
50
51           List<String> argList = new ArrayList<>();
52
53           for (String arg: args) {
54               argList.add(arg);
55           }
56
57           //-P
58           argList.remove(0);
59
60           //<product-name>
61           String product = argList.remove(0);
62
63           URL oclipUrl = new URL(oclipHome);
64           OpenInterfaceGrpcClient client = new OpenInterfaceGrpcClient(
65                   oclipUrl.getHost(), oclipUrl.getPort());
66
67           try {
68               Result result = client.remoteCli(Args.newBuilder().addAllArgs(argList).setProduct(product).build());
69               return result;
70           } finally {
71             client.shutdown();
72           }
73     }
74
75
76     public static void main(String[] args) throws Exception {
77         int exitCode = 1;
78         try {
79             Result result = OpenRemoteCli.run(args);
80             System.out.println(result.getOutput());
81             exitCode = result.getExitCode();
82         } finally {
83             System.exit(exitCode);
84           }
85     }
86 }