001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2017 MicroBean.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014 * implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.microbean.helm.maven;
018
019import java.util.regex.Matcher;
020
021import org.apache.maven.plugins.annotations.Parameter;
022
023import org.microbean.helm.ReleaseManager;
024
025/**
026 * An {@link AbstractReleaseMojo} that works on exactly one <a
027 * href="https://docs.helm.sh/glossary/#release">Helm release</a>.
028 *
029 * @author <a href="https://about.me/lairdnelson"
030 * target="_parent">Laird Nelson</a>
031 */
032public abstract class AbstractSingleReleaseMojo extends AbstractReleaseMojo {
033
034
035  /*
036   * Instance fields.
037   */
038
039
040  /**
041   * The name of the release.
042   */
043  @Parameter(required = true, property = "helm.releaseName")
044  private String releaseName;
045
046
047  /*
048   * Constructors.
049   */
050  
051
052  /**
053   * Creates a new {@link AbstractSingleReleaseMojo}.
054   */
055  protected AbstractSingleReleaseMojo() {
056    super();
057  }
058
059
060  /*
061   * Instance methods.
062   */
063  
064
065  /**
066   * Returns the release name.
067   *
068   * <p>This method may return {@code null}.</p>
069   *
070   * <p>Overrides of this method may return {@code null}.</p>
071   *
072   * @return the release name, or {@code null}
073   *
074   * @see #setReleaseName(String)
075   */
076  public String getReleaseName() {
077    return this.releaseName;
078  }
079
080  /**
081   * Sets the release name.
082   *
083   * @param releaseName the name of the release; must not be {@code
084   * null} and must {@linkplain #validateReleaseName(String) pass
085   * validation}
086   *
087   * @see #getReleaseName()
088   */
089  public void setReleaseName(final String releaseName) {
090    validateReleaseName(releaseName);
091    this.releaseName = releaseName;
092  }
093
094  /**
095   * Validates the supplied {@code name} as a <a
096   * href="https://docs.helm.sh/glossary/#release">Helm release</a>
097   * name.
098   *
099   * <p>This implementation checks to see if the supplied {@code name}
100   * is non-{@code null}, not {@linkplain String#isEmpty() empty}, and
101   * {@linkplain Matcher#matches() matches} the value of the {@link
102   * ReleaseManager#DNS_SUBDOMAIN_PATTERN} field.</p>
103   *
104   * @param name the release name to validate; must not be {@code
105   * null}
106   *
107   * @exception IllegalArgumentException if {@code name} is not valid
108   */
109  protected void validateReleaseName(final String name) {
110    if (name == null) {
111      throw new IllegalArgumentException("Invalid release name: null");
112    } else if (name.isEmpty()) {
113      throw new IllegalArgumentException("Invalid release name: ");
114    } else {
115      final Matcher matcher = ReleaseManager.DNS_SUBDOMAIN_PATTERN.matcher(name);
116      assert matcher != null;
117      if (!matcher.matches()) {
118        throw new IllegalArgumentException("Invalid release name: " + name + "; must match " + ReleaseManager.DNS_SUBDOMAIN_PATTERN.toString());
119      }
120    }
121  }
122  
123}