-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: input config files from http links (#1056)
Signed-off-by: Mehant Kammakomati <[email protected]>
- Loading branch information
Showing
6 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright IBM Corporation 2023 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package download | ||
|
||
import ( | ||
"github.com/konveyor/move2kube/common" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// DownloadOptions stores options for the downloader | ||
type DownloadOptions struct { | ||
ContentURL string | ||
DownloadDestinationPath string | ||
Overwrite bool | ||
} | ||
|
||
// Downloader defines interface for downloaders | ||
type Downloader interface { | ||
Download(DownloadOptions) (string, error) | ||
} | ||
|
||
// IsRemotePath checks if the provided string is a valid remote path or not | ||
func IsRemotePath(str string) bool { | ||
return common.IsHTTPURL(str) | ||
} | ||
|
||
// GetDownloadedPath downloads the content using suitable downloader and then returns the downloaded file path | ||
func GetDownloadedPath(contentURL string, downloadDestinationPath string, overwrite bool) string { | ||
var err error | ||
downloadedPath := "" | ||
if common.IsHTTPURL(contentURL) { | ||
content := HTTPContent{} | ||
downloadOpts := DownloadOptions{ContentURL: contentURL, DownloadDestinationPath: downloadDestinationPath, Overwrite: overwrite} | ||
downloadedPath, err = content.Download(downloadOpts) | ||
if err != nil { | ||
logrus.Fatalf("failed to download the content using http downloader. Error : %+v", err) | ||
} | ||
} else { | ||
logrus.Fatalf("other downloader backends are not currently supported.") | ||
} | ||
return downloadedPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright IBM Corporation 2023 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package download | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// HTTPContent stores remote content config | ||
type HTTPContent struct { | ||
ContentFilePath string | ||
} | ||
|
||
// Download downloads content from the given content URL | ||
func (content *HTTPContent) Download(downloadOptions DownloadOptions) (string, error) { | ||
if downloadOptions.DownloadDestinationPath == "" { | ||
return "", fmt.Errorf("the path where the content has to be downloaded is empty - %s", downloadOptions.DownloadDestinationPath) | ||
} | ||
|
||
_, err := os.Stat(downloadOptions.DownloadDestinationPath) | ||
if os.IsNotExist(err) { | ||
logrus.Debugf("downloaded content would be available at '%s'", downloadOptions.DownloadDestinationPath) | ||
} else if downloadOptions.Overwrite { | ||
logrus.Infof("downloaded content will be overwritten at %s", downloadOptions.DownloadDestinationPath) | ||
err = os.RemoveAll(downloadOptions.DownloadDestinationPath) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to remove the directory at the given path - %s. Error : %+v", downloadOptions.DownloadDestinationPath, err) | ||
} | ||
} else { | ||
return downloadOptions.DownloadDestinationPath, nil | ||
} | ||
logrus.Infof("Downloading the content using http downloader into %s. This might take some time.", downloadOptions.DownloadDestinationPath) | ||
|
||
out, err := os.Create(downloadOptions.DownloadDestinationPath) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create a file for the provided path - %s. Error : %+v", downloadOptions.DownloadDestinationPath, err) | ||
} | ||
defer out.Close() | ||
|
||
resp, err := http.Get(downloadOptions.ContentURL) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to http get content from the provided content url - %s. Error : %+v", downloadOptions.ContentURL, err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("failed to http get content from the provided content url - %s. Received status code %d", downloadOptions.ContentURL, resp.StatusCode) | ||
} | ||
|
||
_, err = io.Copy(out, resp.Body) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to copy content from response to file. Error : %+v", err) | ||
} | ||
content.ContentFilePath = downloadOptions.DownloadDestinationPath | ||
return downloadOptions.DownloadDestinationPath, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters