# -*- coding: utf-8 -*- #
# Copyright 2019 Google LLC. All Rights Reserved.
#
# Licensed 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.
"""Fetch Anthos package from a git repository."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals


from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.anthos import anthoscli_backend
from googlecloudsdk.command_lib.anthos import flags


class Get(base.BinaryBackedCommand):
  """Fetch Anthos package from a git repository."""

  detailed_help = {
      'EXAMPLES': """
      To fetch package FOO from github into local package dir
      `my-foo`:

          $ {command} https://github.com/myaccount/foo.git --local-dir my-foo

      To fetch package myaccount/foo.git/other_package from github,
      with  tag 'v123' into local package dir 'my-foo':

          $ {command} https://github.com/myaccount/foo.git/other_package@v123 --local-dir my-foo
            """,
  }

  @staticmethod
  def Args(parser):
    flags.GetRepoURIFlag().AddToParser(parser)
    flags.GetLocalDirFlag(positional=False).AddToParser(parser)
    flags.GetFilePatternFlag().AddToParser(parser)

  def Run(self, args):
    command_executor = anthoscli_backend.AnthosCliWrapper()
    response = command_executor(command='get',
                                repo_uri=args.REPO_URI,
                                local_dest=args.LOCAL_DIR,
                                env=anthoscli_backend.GetEnvArgsForCommand(),
                                show_exec_error=args.show_exec_error,
                                file_pattern=args.PATTERN)
    return self._DefaultOperationResponseHandler(response)
