Mmuzaf commented on code in PR #2497:
URL: https://github.com/apache/cassandra/pull/2497#discussion_r2197774115


##########
src/java/org/apache/cassandra/tools/NodeTool.java:
##########
@@ -356,208 +217,52 @@ protected void err(Throwable e)
         output.err.println(getStackTraceAsString(e));
     }
 
-    public static class CassHelp extends Help implements NodeToolCmdRunnable
-    {
-        public void run(INodeProbeFactory nodeProbeFactory, Output output)
-        {
-            run();
-        }
-    }
-
-    interface NodeToolCmdRunnable
+    private enum CliLayout
     {
-        void run(INodeProbeFactory nodeProbeFactory, Output output);
+        AIRLINE,
+        PICOCLI
     }
 
-    public static abstract class NodeToolCmd implements NodeToolCmdRunnable
+    private static class CassandraCliFactory implements CommandLine.IFactory
     {
+        private final CommandLine.IFactory fallback;
+        private final INodeProbeFactory nodeProbeFactory;
+        private final Output output;
 
-        @Option(type = OptionType.GLOBAL, name = {"-h", "--host"}, description 
= "Node hostname or ip address")
-        private String host = "127.0.0.1";
-
-        @Option(type = OptionType.GLOBAL, name = {"-p", "--port"}, description 
= "Remote jmx agent port number")
-        private String port = "7199";
-
-        @Option(type = OptionType.GLOBAL, name = {"-u", "--username"}, 
description = "Remote jmx agent username")
-        private String username = EMPTY;
-
-        @Option(type = OptionType.GLOBAL, name = {"-pw", "--password"}, 
description = "Remote jmx agent password")
-        private String password = EMPTY;
-
-        @Option(type = OptionType.GLOBAL, name = {"-pwf", "--password-file"}, 
description = "Path to the JMX password file")
-        private String passwordFilePath = EMPTY;
-
-        @Option(type = OptionType.GLOBAL, name = { "-pp", "--print-port"}, 
description = "Operate in 4.0 mode with hosts disambiguated by port number", 
arity = 0)
-        protected boolean printPort = false;
-
-        private INodeProbeFactory nodeProbeFactory;
-        protected Output output;
-
-        @Override
-        public void run(INodeProbeFactory nodeProbeFactory, Output output)
+        public CassandraCliFactory(INodeProbeFactory nodeProbeFactory, Output 
output)
         {
+            this.fallback = CommandLine.defaultFactory();
             this.nodeProbeFactory = nodeProbeFactory;
             this.output = output;
-            runInternal();
         }
 
-        public void runInternal()
+        public <K> K create(Class<K> cls)
         {
-            if (isNotEmpty(username)) {
-                if (isNotEmpty(passwordFilePath))
-                    password = readUserPasswordFromFile(username, 
passwordFilePath);
-
-                if (isEmpty(password))
-                    password = promptAndReadPassword();
-            }
-
-            try (NodeProbe probe = connect())
-            {
-                execute(probe);
-                if (probe.isFailed())
-                    throw new RuntimeException("nodetool failed, check server 
logs");
-            }
-            catch (IOException e)
-            {
-                throw new RuntimeException("Error while closing JMX 
connection", e);
-            }
-
-        }
-
-        private String readUserPasswordFromFile(String username, String 
passwordFilePath) {
-            String password = EMPTY;
-
-            File passwordFile = new File(passwordFilePath);
-            try (Scanner scanner = new 
Scanner(passwordFile.toJavaIOFile()).useDelimiter("\\s+"))
+            try
             {
-                while (scanner.hasNextLine())
+                K bean = this.fallback.create(cls);
+                Class<?> beanClass = bean.getClass();
+                do
                 {
-                    if (scanner.hasNext())
+                    Field[] fields = beanClass.getDeclaredFields();
+                    for (Field field : fields)
                     {
-                        String jmxRole = scanner.next();
-                        if (jmxRole.equals(username) && scanner.hasNext())
-                        {
-                            password = scanner.next();
-                            break;
-                        }
+                        if (!field.isAnnotationPresent(Inject.class))
+                            continue;
+                        field.setAccessible(true);

Review Comment:
   Fixed.



##########
src/java/org/apache/cassandra/tools/nodetool/AbstractCommand.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *     http://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 org.apache.cassandra.tools.nodetool;
+
+import javax.inject.Inject;
+
+import org.apache.cassandra.tools.NodeProbe;
+import org.apache.cassandra.tools.Output;
+import picocli.CommandLine.ExecutionException;
+
+/**
+ * Abstract class for all nodetool commands, which provides common methods and 
fields.
+ * <p>
+ * The command is executed by calling {@link #execute(NodeProbe)}, in all 
other cases
+ * it should not contain any fields or methods that are specific to a 
particular API
+ * being executed, or common methods that are shared across multiple commands.
+ * <p>
+ * Commands must be API-agnostic and work only with the {@link NodeProbe} API, 
or a

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to