All files / modules/75-ce/components/DownloadCLI DownloadCLI.tsx

94.12% Statements 32/34
66.67% Branches 12/18
85.71% Functions 6/7
96.97% Lines 32/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114              6x 6x 6x 6x 6x 6x   6x 6x 6x 6x 6x     6x 6x 6x 6x     6x                                                             6x 7x 6x 6x   6x 3x               3x         6x 1x 1x 1x 1x     6x 1x   6x                                                 6x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import { useParams } from 'react-router-dom'
import { isEmpty as _isEmpty } from 'lodash-es'
import { Button, Layout, Select, SelectOption, Text } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import css from './DownloadCLI.module.scss'
 
enum OS {
  Mac = 'Mac',
  Windows = 'Windows',
  Linux = 'Linux',
  Darwin = 'darwin'
}
 
enum Arch {
  AMD64 = 'amd64',
  ARM64 = 'arm64',
  A386 = 386
}
 
const dropdownOptions: SelectOption[] = [
  {
    label: `${OS.Mac} (${Arch.AMD64})`,
    value: `${OS.Darwin}-${Arch.AMD64}`
  },
  {
    label: `${OS.Mac} (${Arch.ARM64})`,
    value: `${OS.Darwin}-${Arch.ARM64}`
  },
  {
    label: `${OS.Linux} (${Arch.AMD64})`,
    value: `${OS.Linux.toLowerCase()}-${Arch.AMD64}`
  },
  {
    label: `${OS.Linux} (${Arch.ARM64})`,
    value: `${OS.Linux.toLowerCase()}-${Arch.ARM64}`
  },
  {
    label: `${OS.Linux} (${Arch.A386})`,
    value: `${OS.Linux.toLowerCase()}-${Arch.A386}`
  },
  {
    label: `${OS.Windows} (${Arch.A386})`,
    value: `${OS.Windows.toLowerCase()}-${Arch.A386}`
  },
  {
    label: `${OS.Windows} (${Arch.AMD64})`,
    value: `${OS.Windows.toLowerCase()}-${Arch.AMD64}`
  }
]
 
const DownloadCLI: React.FC = () => {
  const { getString } = useStrings()
  const { accountId } = useParams<{ accountId: string }>()
  const [assetLink, setAssetLink] = React.useState<SelectOption | null>(null)
 
  React.useEffect(() => {
    const defaultOs = navigator.appVersion.includes(OS.Mac.valueOf())
      ? OS.Mac
      : navigator.appVersion.includes(OS.Windows.valueOf())
      ? OS.Windows
      : navigator.appVersion.includes(OS.Linux.valueOf())
      ? OS.Linux
      : null
 
    Iif (defaultOs) {
      setAssetLink(dropdownOptions.find(item => item.label.includes(defaultOs)) as SelectOption)
    }
  }, [])
 
  const downloadAsset = async () => {
    const [platform, arch] = ((assetLink?.value || '') as string).split('-')
    const linkEl: HTMLAnchorElement = document.createElement('a')
    linkEl.href = `${window.location.origin}/gateway/lw/api/internal/downloads/harness_cli?platform=${platform}&arch=${arch}&accountIdentifier=${accountId}`
    linkEl.click()
  }
 
  const handleOsSelectChange = (option: SelectOption) => {
    setAssetLink(option)
  }
  return (
    <div>
      <Text className={css.text}>
        {getString('ce.co.sshSetup')}
        <span>
          <a
            href=" https://ngdocs.harness.io/article/7025n9ml7z-create-autostopping-rules-aws#setup_access_using_ssh_rdp"
            target="_blank"
          >
            Read More
          </a>
        </span>
      </Text>
      <Layout.Horizontal className={css.downloadCliContainer}>
        <div className={css.selectContainer}>
          <Select items={dropdownOptions} onChange={handleOsSelectChange} value={assetLink} name={'sshOs'} />
        </div>
        <Button className={css.downloadBtn} onClick={downloadAsset} disabled={_isEmpty(assetLink)}>
          {'Download CLI'}
        </Button>
      </Layout.Horizontal>
    </div>
  )
}
 
export default DownloadCLI