1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 com.android.commands.incident;
18 
19 import android.util.Log;
20 
21 import com.android.commands.incident.sections.PersistLogSection;
22 
23 import java.io.PrintStream;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.ListIterator;
29 
30 /**
31  * Helper command runner for incidentd to run customized command to gather data for a non-standard
32  * section.
33  */
34 public class IncidentHelper {
35     private static final String TAG = "IncidentHelper";
36     private static boolean sLog = false;
37     private final List<String> mArgs;
38     private ListIterator<String> mArgsIterator;
39 
IncidentHelper(String[] args)40     private IncidentHelper(String[] args) {
41         mArgs = Collections.unmodifiableList(Arrays.asList(args));
42         mArgsIterator = mArgs.listIterator();
43     }
44 
showUsage(PrintStream out)45     private static void showUsage(PrintStream out) {
46         out.println("This command is not designed to be run manually.");
47         out.println("Usage:");
48         out.println("  run [sectionName]");
49     }
50 
run(String[] args)51     private void run(String[] args) throws ExecutionException {
52         Section section = null;
53         List<String> sectionArgs = new ArrayList<>();
54         while (mArgsIterator.hasNext()) {
55             String arg = mArgsIterator.next();
56             if ("-l".equals(arg)) {
57                 sLog = true;
58                 Log.i(TAG, "Args: [" + String.join(",", args) + "]");
59             } else if ("run".equals(arg)) {
60                 section = getSection(nextArgRequired());
61                 mArgsIterator.forEachRemaining(sectionArgs::add);
62                 break;
63             } else {
64                 log(Log.WARN, TAG, "Error: Unknown argument: " + arg);
65                 return;
66             }
67         }
68         section.run(System.in, System.out, sectionArgs);
69     }
70 
getSection(String name)71     private static Section getSection(String name) throws IllegalArgumentException {
72         if ("persisted_logs".equals(name)) {
73             return new PersistLogSection();
74         }
75         throw new IllegalArgumentException("Section not found: " + name);
76     }
77 
nextArgRequired()78     private String nextArgRequired() {
79         if (!mArgsIterator.hasNext()) {
80             throw new IllegalArgumentException(
81                     "Arg required after \"" + mArgs.get(mArgsIterator.previousIndex()) + "\"");
82         }
83         return mArgsIterator.next();
84     }
85 
86     /**
87      * Print the given message to stderr, also log it if asked to (set by -l cmd arg).
88      */
log(int priority, String tag, String msg)89     public static void log(int priority, String tag, String msg) {
90         System.err.println(tag + ": " + msg);
91         if (sLog) {
92             Log.println(priority, tag, msg);
93         }
94     }
95 
96     /**
97      * Command-line entry point.
98      *
99      * @param args The command-line arguments
100      */
main(String[] args)101     public static void main(String[] args) {
102         if (args.length == 0) {
103             showUsage(System.err);
104             System.exit(0);
105         }
106         IncidentHelper incidentHelper = new IncidentHelper(args);
107         try {
108             incidentHelper.run(args);
109         } catch (IllegalArgumentException e) {
110             showUsage(System.err);
111             System.err.println();
112             e.printStackTrace(System.err);
113             if (sLog) {
114                 Log.e(TAG, "Error: ", e);
115             }
116         } catch (Exception e) {
117             e.printStackTrace(System.err);
118             if (sLog) {
119                 Log.e(TAG, "Error: ", e);
120             }
121             System.exit(1);
122         }
123     }
124 }
125