001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.common.hardware;
019
020import java.io.IOException;
021import java.nio.file.Files;
022import java.nio.file.Path;
023import java.util.Optional;
024import org.photonvision.common.logging.LogGroup;
025import org.photonvision.common.logging.Logger;
026
027/**
028 * Our blessed images inject the current version via this build workflow:
029 * https://github.com/PhotonVision/photon-image-modifier/blob/2e5ddb6b599df0be921c12c8dbe7b939ecd7f615/.github/workflows/main.yml#L67
030 *
031 * <p>This class provides a convenient abstraction around this
032 */
033public class OsImageVersion {
034    private static final Logger logger = new Logger(OsImageVersion.class, LogGroup.General);
035
036    private static Path imageVersionFile = Path.of("/opt/photonvision/image-version");
037
038    public static final Optional<String> IMAGE_VERSION = getImageVersion();
039
040    private static Optional<String> getImageVersion() {
041        if (!imageVersionFile.toFile().exists()) {
042            logger.warn(
043                    "Photon cannot locate base OS image version metadata at " + imageVersionFile.toString());
044            return Optional.empty();
045        }
046
047        try {
048            return Optional.of(Files.readString(imageVersionFile).strip());
049        } catch (IOException e) {
050            logger.error("Couldn't read image-version file", e);
051        }
052
053        return Optional.empty();
054    }
055}