View Javadoc

1   package com.atlassian.amps.sdk;
2   
3   import java.io.IOException;
4   import java.net.PasswordAuthentication;
5   
6   import com.atlassian.amps.sdk.mpac.MpacPublisher;
7   import com.atlassian.marketplace.client.MpacException;
8   
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  
12  import joptsimple.OptionParser;
13  import joptsimple.OptionSet;
14  
15  import static java.util.Arrays.asList;
16  
17  /**
18   * Created by Lucas Le on 5 Aug 2015.
19   */
20  public class Main
21  {
22      final private static Logger LOG = LoggerFactory.getLogger(Main.class);
23      final private static String MPAC_USERNAME = "mpacUsername";
24      final private static String MPAC_PASSWORD = "mpacPassword";
25      final private static String ECO_USERNAME = "ecoUsername";
26      final private static String ECO_PASSWORD = "ecoPassword";
27      final private static String RELEASE_VERSION = "releaseVersion";
28  
29      public static void main(String[] args)
30      {
31          final OptionParser parser = new SDKParser();
32          final OptionSet options = parser.parse(args);
33          if (options.has("h") || options.has("?") || !options.hasOptions())
34          {
35              try
36              {
37                  parser.printHelpOn(System.out);
38              }
39              catch (IOException e)
40              {
41                  LOG.error(e.getMessage());
42              }
43              System.exit(0);
44          }
45          if (!isOptionsValid(options))
46          {
47              System.exit(0);
48          }
49          final PasswordAuthentication mpacCredentials = new PasswordAuthentication(options.valuesOf(MPAC_USERNAME).toString(),
50                  options.valueOf(MPAC_PASSWORD).toString().toCharArray());
51          final PasswordAuthentication ecoCredentials = new PasswordAuthentication(options.valueOf(ECO_USERNAME).toString(),
52                  options.valueOf(ECO_PASSWORD).toString().toCharArray());
53          try
54          {
55              MpacPublisher.MpacPublisherBuilder.aMpacPublisher()
56                      .mpacCredentials(mpacCredentials)
57                      .ecoCredentials(ecoCredentials)
58                      .version(options.valueOf(RELEASE_VERSION).toString())
59                      .build()
60                      .publish();
61          }
62          catch (IOException e)
63          {
64              LOG.error(e.getMessage());
65          }
66          catch (MpacException e)
67          {
68              LOG.error("MPAC publisher could not publish Atlassian Plugin SDK, please check your MPAC credentials and server url again", e);
69          }
70      }
71  
72      private static boolean isOptionsValid(OptionSet options)
73      {
74          boolean isOptionsValid = true;
75          if (!options.has(MPAC_USERNAME))
76          {
77              LOG.info("MPAC username could not be empty --" + MPAC_USERNAME);
78              isOptionsValid = false;
79          }
80          if (!options.has(MPAC_PASSWORD))
81          {
82              LOG.info("MPAC password could not be empty --" + MPAC_PASSWORD);
83              isOptionsValid = false;
84          }
85          if (!options.has(ECO_USERNAME))
86          {
87              LOG.info("Ecosystem username could not be empty --" + ECO_USERNAME);
88              isOptionsValid = false;
89          }
90          if (!options.has(ECO_PASSWORD))
91          {
92              LOG.info("Ecosystem password could not be empty --" + ECO_PASSWORD);
93              isOptionsValid = false;
94          }
95          if (!options.has(RELEASE_VERSION))
96          {
97              LOG.info("Atlassian Plugin SDK release version could not be empty --" + RELEASE_VERSION);
98              isOptionsValid = false;
99          }
100         return isOptionsValid;
101     }
102 
103     private static class SDKParser extends OptionParser
104     {
105         {
106             accepts(MPAC_USERNAME).withRequiredArg().describedAs("MPAC username");
107             accepts(MPAC_PASSWORD).withRequiredArg().describedAs("MPAC password");
108             accepts(ECO_USERNAME).withRequiredArg().describedAs("Ecosystem username");
109             accepts(ECO_PASSWORD).withRequiredArg().describedAs("Ecosystem password");
110             accepts(RELEASE_VERSION).withRequiredArg().describedAs("Atlassian plugin SDK release version");
111             acceptsAll(asList("h", "?"), "show help").forHelp();
112         }
113     }
114 }